PHP

Debug variables in Yii 2

I’ve been working with Yii framework version 2.0 for a few days now, and I find Yii’s Debugger and vardumper function are extremely uncomfortable!

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.

Installation:

  • Create a file so called functions.php in

/common/config/

2

  • Paste those code into function.php then save it (please re-format those lines of code, stupid wp.com >:( )
<?php
/*
* Code by DangNH
* Date: Apr 4, 2016
*/
 
/**
* Debug function
* d($var);
*/
function d() {
 echo '<pre>';
 for ($i = 0; $i < func_num_args(); $i++) {
 yii\helpers\VarDumper::dump(func_get_arg($i), 10, true);
 }
 echo '</pre>';
}
 
/**
* Debug function with die() after
* dd($var);
*/
function dd() {
 for ($i = 0; $i < func_num_args(); $i++) {
  d(func_get_arg($i));
 }
 die();
}
  • Now, edit bootstrap.php in

/common/config/bootstrap.php

3

and then add this line of code to the top:

 

require('functions.php'); //add custom helper functions to the whole application

like so

4

Voila! Now you can call d($var1, $var2, $varx); for dump or dd($var1, $var2, $varx) for dump then die anywhere in your application!

Sample result:

Reply