How do I dump variables in Yii for debugging?
Solution 1
Because you are asking about something like var_dump
and print_r
, I can advise built-in helper for this. It's called yii\helpers\VarDumper. Yii::trace() is for logging trace messages.
VarDumper is intended to replace the buggy PHP function
var_dump
andprint_r
.It can correctly identify the recursively referenced objects in a complex object structure. It also has a recursive depth control to avoid indefinite recursive display of some peculiar variables.
VarDumper can be used as follows,
VarDumper::dump($var);
Personally I don't use it, just tried only couple of times for testing.
I think is better to use Xdebug for that purposes.
See also PsySH.
Solution 2
Use this:
<?php echo '<pre>'; print_r($model); exit; ?>
Solution 3
Use this, to see your variable or object array.
use yii\helpers\VarDumper;
VarDumper::dump($variableArray , $dept = 10, $highlight = true);
To detail you can read the doc http://www.yiiframework.com/doc-2.0/yii-helpers-basevardumper.html#dump()-detail
Solution 4
I used this, but I'm sure there's a better way.
Yii::warning('**********************', var_export($category,true));
config/web.php
'log' => [
...
'flushInterval' => 1, // for debug
'targets' => [
[
...
'exportInterval' => 1, // for debug - slow
],
],
],
Solution 5
You can do it yourself:
In your main index page (back/index.php
or front/index.php
) add this code to the top ob_start();
. Then define 2 functions for better debugging
function dd($v){
ob_clean();
var_dump($v);
exit;
}
function dj($v){
ob_clean();
echo CJSON::encode($v);
exit;
}
and add ob_end_flush();
at the very end of your main index page . Now you can call dd($model)
or dj($model)
. There you had your dumper working. Congratulations!
Chloe
Updated on July 09, 2022Comments
-
Chloe almost 2 years
How do I dump and print variables in Yii for debugging? I would like to use
var_dump()
orprint_r()
. I tried to useYii::trace()
but it crashes with this error inruntime/logs/app.log
. It doesn't even tell me the line in my code that it fails.2015-03-18 20:54:11 [::1][-][-][warning][yii\log\Dispatcher::dispatch] Unable to send log via yii\debug\LogTarget: Exception 'Exception' with message 'Serialization of 'SimpleXMLElement' is not allowed' in /cygdrive/c/Users/Chloe/workspace/AffiliateArbitrage/vendor/yiisoft/yii2-debug/LogTarget.php:58 Stack trace: #0 /cygdrive/c/Users/Chloe/workspace/AffiliateArbitrage/vendor/yiisoft/yii2-debug/LogTarget.php(58): serialize(Array) #1 /cygdrive/c/Users/Chloe/workspace/AffiliateArbitrage/vendor/yiisoft/yii2-debug/LogTarget.php(112): yii\debug\LogTarget->export(Array) #2 /cygdrive/c/Users/Chloe/workspace/AffiliateArbitrage/vendor/yiisoft/yii2/log/Dispatcher.php(183): yii\debug\LogTarget->collect(Array, true) #3 /cygdrive/c/Users/Chloe/workspace/AffiliateArbitrage/vendor/yiisoft/yii2/log/Logger.php(170): yii\log\Dispatcher->dispatch(Array, true) #4 [internal function]: yii\log\Logger->flush(true) #5 {main}
Reference http://www.yiiframework.com/doc-2.0/guide-runtime-logging.html
-
Chloe about 9 yearsPerfect! Old school debugging! Except that stupid default nav bar floating at the top that obscures the output...
-
arogachev about 9 yearsAdd
exit
ordie
right after dump the same as with regularvar_dump
andprint_r
. -
arogachev about 9 yearsAdded link to another debugger that probably will help you more with this.
-
Chloe about 9 yearsPsy SH looks really neat but there's probably something wrong with it. Composer didn't install any executable. Not only that, it's not clear how to connect the shell to the web server. It says it will drop into a shell if you add the debug line, but PHP is running in Apache in the background! How will it show a shell? There's no mention of setting a port or anything for remote debugging.
-
user7866688 about 7 yearsecho '<pre>'; print_r($model); exit; Tnks
-
user7866688 about 7 years<?php echo '<pre>';print_r($model);exit; ?>
-
Fabio says Reinstate Monica about 7 yearsHello, welcome to SO. If you realize you have forgotten something, or made a mistake, you should edit your answer, not leave it as a comment. Comments are not read by all, they could be deleted, and do not allow proper code formatting. Another user has already suggested an edit to fix it: please review it, edit your answer further if you think it's needed, and then remove the comments. Thank you!
-
Roby Sottini about 6 yearsWhere can I see the message?
-
Amil Waduwawara over 5 yearsActually I wanted to see messages almost immediately in log files for Yii commands. This helped me, thanks a lot