Baking Cakes with FirePHP

By Tobias Funke (Warringer)
Some days ago I discovered the Firebug extension FirePHP while looking around for Firefox addons. The way to use Firebug to log Error and other general debugging messages looked very promising and I decided to add it to my current Project.
Of course you need some things to use FirePHP.

  1. Firefox 2 or 3(I guess everyone knows where to find it... :) )
  2. Firebug http://getfirebug.com/ 1.0 or 1.1 for Firefox 2, 1.2 for Firefox 3
  3. FirePHP plugin for Firebug https://addons.mozilla.org/en-US/firefox/addon/6149 or http://www.firephp.org/
  4. FirePHP Core library for PHP http://www.firephp.org/

NOTE: You must have the Firebug Net panel enabled for FirePHP to work.
New NOTE 2: FirePHP works only for PHP 5.

For general information about FirePHP you should visit http://www.firephp.org/ anyway.

Please note that the following way to use FirePHP is very basic at the moment and pretty much not more than a quick 'hack'.

The first is to put the file FirePHP.class.php into /app/vendors folder.

The next if to make a copy of dbo_source.php and put it into /app/models/datasources to keep the core of Cake untouched.

Now you just need to replace showLog() in your copy of dbo_source.php with the following.

Download code <?php 
/**
 * Outputs the contents of the queries log.
 *
 * @param boolean $sorted
 */
    
function showLog($sorted false) {
        if (
$sorted) {
            
$log sortByKey($this->_queriesLog'took''desc'SORT_NUMERIC);
        } else {
            
$log $this->_queriesLog;
        }

        if (
$this->_queriesCnt 1) {
            
$text 'queries';
        } else {
            
$text 'query';
        }

        if (
php_sapi_name() != 'cli') {
            
$summery "{$this->_queriesCnt} {$text} took {$this->_queriesTime} ms";
            
$header = array("Nr""Query""Error""Affected""Num. rows""Took (ms)");
            
$body = array($header);
            foreach (
$log as $k => $i) {
                
$row = array(($k 1), $i['query'], $i['error'], $i['affected'], $i['numRows'], $i['took']);
                
$body[] = $row;
                }
            
fb(array($summery$body), FirePHP::TABLE);
            } else {
            foreach (
$log as $k => $i) {
                print ((
$k 1) . ". {$i['query']} {$i['error']}\n");
            }
        }
    }
?>

Next you modify your bootstrap.php with the following:

Download code <?php 
ob_start
();
App:: import 'Vendor''FirePHP', array ( 'file' => 'FirePHP.class.php'));
function 
fb() 
{
  
$instance FirePHP::getInstance(true);
  
$args func_get_args();
  return 
call_user_func_array(array($instance,'fb'),$args);
  return 
true;
}
?>

FirePHP requires output buffering and fb() is for convenience... :)

Now enjoy your baking with some more fire...

Through of course other Debugging can be done with FirePHP as well, aside from logging Database access, like in this example.

I'm pretty sure it can be turned into a Plugin, but I have to say that I'm a little to new to Cake to be able to create one...

 

Comments 783

CakePHP Team Comments Author Comments
 

Comment

1 Awesome

I have to try this out! thanks for sharing this - didn't even know firePHP existed.
Posted Aug 26, 2008 by Brit Gardner
 

Comment

2 PHP4

Hey, i'm testing the code, but having headaches with PHP4 and the definition of const at the beginning of the class.

Do you have a quick solution? alternate library for php4? My hosting will keep me here for a while.. :(

Posted Aug 27, 2008 by Adrian Toro
 

Comment

3 Nice one

I also didn't know it existed. But very useful. Now someone have to turn it in a plug-in :).

Thank you!
Posted Sep 1, 2008 by Reijer van Oorspronk
 

Comment

4 FirePHP need for PHP 5

Great job! Now debug info looks much better and don't break html formatting. Hope CakePHP developers will include FirePHP for error logging in core :)

I also added function firedebug() in my /app/config/bootstrap.php:

<?php
function firedebug($var false$showHtml false$showFrom true) {
    
$result '';
    if (
Configure::read() > 0) {
        if (
$showFrom) {
            
$calledFrom debug_backtrace();
            
$result .= substr(r(ROOT""$calledFrom[0]['file']), 1)." (line ".$calledFrom[0]['line']."):";
        }
        
$var print_r($vartrue);
        if (
$showHtml) {
            
$var str_replace('<''&lt;'str_replace('>''&gt;'$var));
        }
        
fb($result."\n".$varFirePHP::INFO);
    }
}
?>
Parameters are same as core debug() function, but it outputs debug data to Firebug console.

Hey, i'm testing the code, but having headaches with PHP4 and the definition of const at the beginning of the class.

Do you have a quick solution? alternate library for php4? My hosting will keep me here for a while.. :(

Looks like FirePHP class officially supports only PHP 5 (requirement in README)
Posted Sep 4, 2008 by Alex
 

Comment

5 Another solution

And what about this?

You can copy file "FirePHP.class.php" and rename to "Firephp.php" to /app/vendors/ and create component in /app/controllers/components/ like this

Component Class:

<?php     
    App
::import('Vendor''Firephp');

    class 
FirephpComponent {
        private 
$instance;
        
        public function 
__construct() {
            
$this->instance FirePHP::getInstance(true);
        }
        
        public function 
__call($name$args) {
            
call_user_func_array(array($this->instance$name), $args);
        }
    }
?>

now you can use it from controller

Controller Class:

<?php 
    
class FirephpController extends AppController {
        var 
$components = array('Firephp');
        
        public function 
debug() {
            
$this->Firephp->error('some error');
        }
    }

?>
Posted Nov 11, 2008 by Marwin
 

Comment

6 Updated & Improved Version

Using this article, and Marwin's comment as a guide, I've posted a somewhat improved version at my blog: http://www.utoxin.name/2009/02/cakephp-firephp/
Posted Feb 13, 2009 by Matthew Walker