Tagged cakephp

Use the awesome dBug class in CakePHP

If you program in PHP, you’re probably annoyed by var_dump, because the output is really difficult to read, especially if the var is a nice big object or array. If you’re used to working in CakePHP, you probably use pr() because it’s slightly better than var_dump.

At work, however, we use a class called dBug, which gives you color-coded variable output with collapsible hierarchy, etc. In other words, a pretty awesome debugging tool. To get this functionality in your Cake app, just do the following:

  1. Download dBug.php from the author’s site and put it in your app/ directory.
  2. In your app_controller.php (or whatever your base class is), add the following line:
    [cc lang=”php”]include_once(“dBug.php”); [/cc]
  3. You can get beautiful dBug output by calling the following in a model, controller, or even view:
    [cc lang=”php”]new dbug($myvariable);[/cc]

That’s it! You’ll probably never use pr() again 🙂

Get Vim syntax highlighting in CakePHP template files

If you use Vim to edit your php files, you’ve probably got some sort of default syntax highlighting for those files that’s helpful in debugging code. Of course, if you use CakePHP, Vim won’t see your .ctp template files as the php/html combination they really are, and you’ll just see plain text. To fix this, you can add the following line into your .vimrc file (usually located in the root of your home directory):

[cc lang=”vim”]autocmd BufRead,BufNewFile *.ctp set filetype=php[/cc]

When you launch Vim, it will now interpret your ctp files as if they were php (which they pretty much are), and you’ll be able to get the same colors and syntax highlighting.