Baking Cakes with FirePHP

This article is also available in the following languages:
By 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.

<?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:

<?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

  • Posted 02/13/09 04:48:56 PM
    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 11/11/08 12:31:35 PM
    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 09/01/08 07:27:33 AM
    I also didn't know it existed. But very useful. Now someone have to turn it in a plug-in :).

    Thank you!
  • Posted 08/27/08 03:34:06 PM
    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 09/04/08 11:42:32 AM
      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 08/26/08 02:11:27 PM
    I have to try this out! thanks for sharing this - didn't even know firePHP existed.

Comments are closed for articles over a year old