debug

Hi all,

I am a CakePHP developer who wanted to try out Yii. I’m excited to see Yii stick with PHP 5 and the benefits it brings, but while trying to develop a simple site I found debugging in Yii to be more difficult than it needs to be. Let me explain what I am use to and if there are other frameworks that use something similar, I’m sure you feel my pain.

In CakePHP, when I want to debug my app I simply set a value in a config file debug=2,1 or 0. 0 = no debugging. 1 means you don’t see query level debugging, 2 you get the full debugging. With that value set to 1 or greater when you want to view the contents of a variable, you simply use a line like: debug($myVar); your variable will be spit out, in the newest version of Cake, in a nicely formatted div at the top of the page out put so you can see what s in the variable without it hidding through out your page or cluttering up the layout.

So I had an idea. why not make something like that in Yii. Well I have not taken the time to make this an extension, one day maybe, but for now I will share what I did it too is not as robust as Cake’s but when I get time and a bit more experience with Yii I may get it there.

First the main.php config file. I added this key to the params




'params'=>array(

		// other params

        //Special debug message;

        'debugContent'=>'',

	),



Then I added this to my main.php layout just below the body tag:




<?php if(!empty(Yii::app()->params['debugContent'])):?>

		<?php echo Yii::app()->params['debugContent'];?>

<?php endif;?>



Finally in the class I’m working on I added this method:




protected function Debug($var){

        $bt = debug_backtrace();

        $dump = new CVarDumper();

        $debug = '<div style="display:block;background-color:gold;border-radius:10px;border:solid 1px brown;padding:10px;z-index:10000;"><pre>';

        $debug .= '<h4>function: '.$bt[1]['function'].'() line('.$bt[0]['line'].')'.'</h4>';

        $debug .=  $dump->dumpAsString($var);

        $debug .= "</pre></div>\n";

        Yii::app()->params['debugContent'] .=$debug;

    }



Now when I want to see variable content in my app I simply place this method call in my code:




$this->Debug($myVar);



When the page renders I get nicely formatted divs with debugging info at the top of my page so it is easy to find and stays out of the way of the rest of the layout.

I hope this helps some others.

Hi Bruce,

I am also a CakePHP developer currently migrating to Yii. I have grown quite accustomed to the cake debug.

You have created it on Yii nicely. Congrats!

There is also this "Knit" extension that displays debugs neatly. You can find it under Yii extensions on Yii website

Cheers