簡介
我看了幾遍關於使用外部庫來調試PHP代碼(如: firePHP)的文章, 讀了這篇文章你會發現在 Yii 中沒有必要使用這些外部庫.
Yii 內建了強大的日誌記錄類別. 如果你閱讀了記錄日誌的文檔, 你可以發現我們可以決定我們希望記錄的日誌, 這正是我們要做的,使用CWebLogRoute 創建一個Yii 版本的FirePHP.
配置
在我們的protected/config/main.php 設定檔中加入設定:
<code><span>'log'</span>=><span>array</span>( <span>'class'</span>=><span>'CLogRouter'</span>, <span>'routes'</span>=><span>array</span>( <span>array</span>( <span>'class'</span>=><span>'CWebLogRoute'</span>, <span>//</span><span>// I include *trace* for the</span><span>// sake of the example, you can include</span><span>//</span><span>// more levels separated by commas</span><span>'levels'</span>=><span>'trace'</span>, <span>//</span><span>// I include *vardump* but you</span><span>// can include more separated by commas</span><span>'categories'</span>=><span>'application.*,system.db.*,vardump'</span>, <span>//</span><span>// This is self-explanatory right?</span><span>'showInFireBug'</span>=><span>true</span> ), ), ),</code>
使用
準備完成,現在讓我們來追蹤一下變數來測試一下, 如下:
<code><span>public</span><span><span>function</span><span>actionTrace</span><span>()</span>{</span><span>$test</span> = <span>'This is a test'</span>; <span>$anotherTest</span> = <span>array</span>(<span>'one'</span>,<span>'two'</span>,<span>'three'</span>); <span>echo</span> Yii::trace(CVarDumper::dumpAsString(<span>$test</span>),<span>'vardump'</span>); <span>echo</span> Yii::trace(CVarDumper::dumpAsString(<span>$anotherTest</span>),<span>'vardump'</span>); <span>echo</span> Yii::trace(CVarDumper::dumpAsString(<span>$this</span>),<span>'vardump'</span>); }</code>
Yii 的FirePHP 函數的FirePHP 上面的程式碼寫起來比較長, 我們來使用一下強的建議, 讓我在index.php 頁面寫一個像FirePHP 函數:
<code><span>public</span><span><span>function</span><span>actionTrace</span><span>()</span> {</span><span>//</span><span>// In your index.php or your globals.php file</span><span><span>function</span><span>fb</span><span>(<span>$what</span>)</span> {</span><span>echo</span> Yii::trace(CVarDumper::dumpAsString(<span>$what</span>), <span>'vardump'</span>); } <span>// </span><span>// using the above examples now we could</span><span>$test</span> = <span>'This is a test'</span>; fb(<span>$test</span>); }</code>
OK, 全部完成了,我們的調試器中沒有使用外部的類.
補充
此方法無需安裝chromephp 插件,firebug瀏覽器皆可使用,opera,chrome,firefox等都適用。
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i
').text(i)); }; $numbering.fadeIn(1700); }); }); 以上就介紹了yii 如何使用 CWebLogRoute 記錄和調試變量,包括了route,eblog方面的內容,希望對PHP教程有興趣的朋友有所幫助。