簡(jiǎn)介
我看了幾遍關(guān)于使用外部庫(kù)來(lái)調(diào)試PHP代碼(如: firePHP)的文章, 讀了這篇文章你會(huì)發(fā)現(xiàn)在 Yii 中沒(méi)有必要使用這些外部庫(kù).
Yii 內(nèi)置了強(qiáng)大的日志記錄類(lèi). 如果你閱讀了記錄日志的文檔, 你可以發(fā)現(xiàn)我們可以決定我們希望記錄的日志, 這正是我們要做的,使用 CWebLogRoute 創(chuàng)建一個(gè) Yii 版本的 FirePHP.
配置
在我們的 protected/config/main.php
配置文件中添加配置:
<pre>
'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'=>'vardump',
//
// This is self-explanatory right?
'showInFireBug'=>true
),
),
</pre>
使用
準(zhǔn)備完畢,現(xiàn)在讓我們來(lái)追蹤一下變量來(lái)測(cè)試一下, 如下:
<pre>
$test = 'This is a test';
$anotherTest = array('one','two','three');
// variable
// please, check the inclusion of the category vardump
// not including the category, it wont display at all.
echo Yii::trace(CVarDumper::dumpAsString($test),'vardump');
// array
echo Yii::trace(CVarDumper::dumpAsString($anotherTest),'vardump');
// object
echo Yii::trace(CVarDumper::dumpAsString($this),'vardump');
</pre>
Yii 的 FirePHP 函數(shù)
上面的代碼寫(xiě)起來(lái)比較長(zhǎng), 我們來(lái)使用一下 強(qiáng)的建議, 讓我在 index.php
頁(yè)面寫(xiě)一個(gè)像 FirePHP 函數(shù):
<pre>
//
// 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);
</pre>
OK, 全部完成了,我們的調(diào)試器中沒(méi)有使用外部的類(lèi).
補(bǔ)充
忘了提了,它還適用于Chrome開(kāi)發(fā)工具控制臺(tái).