如何在Yii中转储变量以进行调试?

时间:2022-02-01 23:15:39

How do I dump and print variables in Yii for debugging? I would like to use var_dump() or print_r(). I tried to use Yii::trace() but it crashes with this error in runtime/logs/app.log. It doesn't even tell me the line in my code that it fails.

如何在Yii中转储和打印变量以进行调试?我希望使用var_dump()或print_r()。我尝试使用Yii::trace(),但是它在运行时/logs/app.log中会因为这个错误而崩溃。它甚至没有告诉我代码中的行它失败了。

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

参考http://www.yiiframework.com/doc - 2.0 - / -指导-运行- logging.html

6 个解决方案

#1


9  

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.

因为您正在询问类似var_dump和print_r之类的内容,所以我可以建议您使用内置的helper。它被称为yii \助手\ VarDumper。trace()是用来记录跟踪消息的。

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

VarDumper用于替换有bug的PHP函数var_dump和print_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可以如下使用:

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.

我认为最好为此目的使用Xdebug。

See also PsySH.

也看到PsySH。

#2


1  

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
            ],
        ],
    ],

#3


0  

Use this:

用这个:

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

#4


0  

Use this, to see your variable or object array.

使用这个,查看变量或对象数组。

use yii\helpers\VarDumper;

使用yii \助手\ VarDumper;

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

转储($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

要详细说明,可以阅读doc http://www.yiiframework.com/doc-2.0/yii-helpers-basevardumper.html#dump()-detail

#5


0  

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

你可以自己做:在你的主索引页(回/索引)。php或front/index.php)将此代码添加到上面的ob_start();。然后定义两个函数,以便更好地调试

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!

并添加ob_end_flush();在主索引页的末尾。现在您可以调用dd($model)或dj($model)。你的翻斗车在那里工作。恭喜你!

#6


-1  

I had written an article about this problem with debug variables in Yii 2:

我在Yii 2中写过一篇关于调试变量问题的文章:

Since for the sake of Simplicity and Quick Development, I’ve created a helper for those who use Yii. You can just call dd($var1, $var2, ….); for dump & die or d($var1, $var2, ….); for dump data.

因为为了简单和快速开发,我为使用Yii的人创建了一个助手。你可以叫dd(var1,var2美元,....);转储与死亡或d(var1,var2美元,....);转储数据。

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

详细信息和安装说明如下:https://dangnhsite.wordpress.com/2016/04/06/variabledebug -in-yii-2/。

#1


9  

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.

因为您正在询问类似var_dump和print_r之类的内容,所以我可以建议您使用内置的helper。它被称为yii \助手\ VarDumper。trace()是用来记录跟踪消息的。

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

VarDumper用于替换有bug的PHP函数var_dump和print_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可以如下使用:

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.

我认为最好为此目的使用Xdebug。

See also PsySH.

也看到PsySH。

#2


1  

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
            ],
        ],
    ],

#3


0  

Use this:

用这个:

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

#4


0  

Use this, to see your variable or object array.

使用这个,查看变量或对象数组。

use yii\helpers\VarDumper;

使用yii \助手\ VarDumper;

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

转储($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

要详细说明,可以阅读doc http://www.yiiframework.com/doc-2.0/yii-helpers-basevardumper.html#dump()-detail

#5


0  

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

你可以自己做:在你的主索引页(回/索引)。php或front/index.php)将此代码添加到上面的ob_start();。然后定义两个函数,以便更好地调试

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!

并添加ob_end_flush();在主索引页的末尾。现在您可以调用dd($model)或dj($model)。你的翻斗车在那里工作。恭喜你!

#6


-1  

I had written an article about this problem with debug variables in Yii 2:

我在Yii 2中写过一篇关于调试变量问题的文章:

Since for the sake of Simplicity and Quick Development, I’ve created a helper for those who use Yii. You can just call dd($var1, $var2, ….); for dump & die or d($var1, $var2, ….); for dump data.

因为为了简单和快速开发,我为使用Yii的人创建了一个助手。你可以叫dd(var1,var2美元,....);转储与死亡或d(var1,var2美元,....);转储数据。

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

详细信息和安装说明如下:https://dangnhsite.wordpress.com/2016/04/06/variabledebug -in-yii-2/。