How to reset variables in Yii for debugging?

How to reset and print variables in Yii for debugging? I would like to use var_dump() or print_r() . I tried using Yii::trace() , but it crashes with this error in runtime runtime/logs/app.log . He 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} 

Link http://www.yiiframework.com/doc-2.0/guide-runtime-logging.html

+5
source share
6 answers

Since you are asking about something like var_dump and print_r , I can recommend a built-in helper for this. It is called yii \ helpers \ VarDumper . Yii :: trace () is for logging trace messages.

VarDumper is intended to replace the PHP buggy function var_dump and print_r .

It can correctly identify recursively related objects in the complex structure of an object. It also has recursive depth control to avoid vague recursive mapping of some special variables.

VarDumper can be used as follows:

 VarDumper::dump($var); 

Personally, I do not use it, I just tried it just a couple of times for testing.

I think it's better to use Xdebug for these purposes.

See also PsySH .

+9
source

I used this, but I'm sure there is a better way.

 Yii::warning('**********************', var_export($category,true)); 
config/web.php
  'log' => [ ... 'flushInterval' => 1, // for debug 'targets' => [ [ ... 'exportInterval' => 1, // for debug - slow ], ], ], 
+1
source

Use this:

 <?php echo '<pre>'; print_r($model); exit; ?> 
0
source

Use this to see a variable or an array of objects.

use yii\helpers\VarDumper;

VarDumper::dump($variableArray , $dept = 10, $highlight = true);

You can read the document http://www.yiiframework.com/doc-2.0/yii-helpers-basevardumper.html#dump()-detail for details

0
source

You can do it yourself: On the index main page ( back/index.php or front/index.php ) add this code to the beginning of 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(); to the very end of the index home page. Now you can call dd($model) or dj($model) . Your dump truck works for you there. Congratulations!

0
source

I wrote an article about this problem with debugging variables in Yii 2:

Since for the sake of simplicity and rapid development, Ive created an assistant for those who use Yii. You can simply call dd($var1, $var2, ….); for dump and die or d($var1, $var2, ….); for dump data.

Details and installation instructions are included: https://dangnhsite.wordpress.com/2016/04/06/variable-debug-in-yii-2/

-1
source

Source: https://habr.com/ru/post/1215636/


All Articles