Baking Cakes with FirePHP
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.
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.
Next you modify your bootstrap.php with the following:
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...
- Firefox 2 or 3(I guess everyone knows where to find it... :) )
- Firebug http://getfirebug.com/ 1.0 or 1.1 for Firefox 2, 1.2 for Firefox 3
- FirePHP plugin for Firebug https://addons.mozilla.org/en-US/firefox/addon/6149 or http://www.firephp.org/
- 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...








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');
}
}
?>
Thank you!
Your prayers have been answered (-; No seriously, I've created (the start of) a plugin for this: http://www.coencoppens.nl/programming/cakephp/firephp-plugin-for-cakephp/. I hope you find it useful (-:
Do you have a quick solution? alternate library for php4? My hosting will keep me here for a while.. :(
I also added function firedebug() in my /app/config/bootstrap.php:
Parameters are same as core debug() function, but it outputs debug data to Firebug console.<?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($var, true);
if ($showHtml) {
$var = str_replace('<', '<', str_replace('>', '>', $var));
}
fb($result."\n".$var, FirePHP::INFO);
}
}
?>
Looks like FirePHP class officially supports only PHP 5 (requirement in README)
Comments are closed for articles over a year old