简介
我看了几遍关于使用外部库来调试PHP代码(如: firePHP)的文章, 读了这篇文章你会发现在 Yii 中没有必要使用这些外部库.
Yii 内置了强大的日志记录类. 如果你阅读了记录日志的文档, 你可以发现我们可以决定我们希望记录的日志, 这正是我们要做的,使用 CWebLogRoute 创建一个 Yii 版本的 FirePHP.
配置
在我们的 protected/config/main.php 配置文件中添加配置:
'log'=>array(
'class'=>'CLogRouter',
'routes'=>array(
array(
'class'=>'CWebLogRoute',
//
// I include *trace* for the
// sake of the example, you can include
//
// more levels separated by commas
'levels'=>'trace',
//
// I include *vardump* but you
// can include more separated by commas
'categories'=>'application.*,system.db.*,vardump',
//
// This is self-explanatory right?
'showInFireBug'=>true ),
),
),
使用
准备完毕,现在让我们来追踪一下变量来测试一下, 如下:
public function actionTrace(){
$test = 'This is a test';
$anotherTest = array('one','two','three');
echo Yii::trace(CVarDumper::dumpAsString($test),'vardump');
echo Yii::trace(CVarDumper::dumpAsString($anotherTest),'vardump');
echo Yii::trace(CVarDumper::dumpAsString($this),'vardump');
}
Yii 的 FirePHP 函数
上面的代码写起来比较长, 我们来使用一下 强的建议, 让我在 index.php 页面写一个像 FirePHP 函数:
public function actionTrace()
{
//
// In your index.php or your globals.php file
function fb($what)
{
echo Yii::trace(CVarDumper::dumpAsString($what), 'vardump');
}
//
// using the above examples now we could
$test = 'This is a test';
fb($test);
}
OK, 全部完成了,我们的调试器中没有使用外部的类.
补充
此方法无需安装chromephp 插件,firebug浏览器均可使用,opera,chrome,firefox等都适用。