Bye-bye $cakeDebug…

The infamous $cakeDebug variable, which has been around for a long time and managed to annoy a few people in the process, has been replaced in the recent builds of CakePHP 1.3 with an element.

See the ticket here:

The two points to take away are:

  1. Automatic SQL dumps have been removed from DboSource
  2. If you wish to display SQL dumps, as before… replace any occurrence of $cakeDebug in your layouts with echo $this->element('sql_dump');

One of the obvious benefits, is that you no longer have to tweak the core to take control of the SQL debug. As always, simply place sql_dump.ctp in your own app (i.e. app/views/elements) and do what you wish with the output.

Just to test things out let’s throw the SQL debug into the Firebug console, rather than directly into the view.

We’ll need to modify the sql_dump.ctp (which now has been copied into our own app), just a little bit.

Around line 35, let’s replace the default output for SQL dump, for something like this:

//unchanged part of the sql_dump.ctp above this line

//here we are outputting the queries into the Firebug console
foreach($logs as $source => $logInfo) {
  foreach($logInfo['log'] as $key => $query) {    
    echo $this->Html->scriptBlock('console.log("' . $query['query'] . '")');  

For the real world example, this may not be a reasonable thing to do by any means… but it does show how easy it is now to output the SQL debug wherever you need it (logging, debugging, stylizing, parsing, sending to some remote destination… up to your imagination really).

Overriding default URL’s (aka persistent routes)

CakePHP’s routes allow for an easy way to serve content by using URL aliases.

Let’s take an example from the manual:

    '/cooks/:action/*', array('controller' => 'users', 'action' => 'index')

By having the user access the URL such as simply shows the default view for (as was our intention).

However, if you have a bunch of links on that page, which point to other actions in the Users controller, the URL’s will remain as In some cases it may not be desired, i.e. if a visitor entered the section of your site by using the “cooks” URL, you don’t want to suddenly confuse them by presenting a completely different link to other actions. Otherwise, you may simply wish to be consistent (or sneaky).

A simple override of the Helper::url() method allows us to solve this problem.
(Place the following in your app_helper.php)

public function url($url = NULL, $full = FALSE) {
  if(strstr($this->params['url']['url'], 'cooks') && $url['controller'] == 'users') {
    $url['controller'] = 'cooks';
  return parent::url($url, $full);

That’s it, now any link that would otherwise point to will now point to

p.s. You might want to add an additional check: isset($url['controller'])