I was poking through the addons for Firebug today, and liked the look of FirePHP, so I started looking into how I could use it. As many of you know, I’m a big fan of CakePHP as a framework, so I looked up how to integrate the two together and make them work nicely. I found this guide on the Cake Bakery, and followed it with a few modifications, so here they are.
First, the requirements:
- Firefox 3+
- Firebug
- FirePHP Extension
- FirePHP Core Library (I installed this from pear)
Copy dbo_source.php (found in cake/lib/models/datasources) from Cake’s core to app/models/datasources/ in your app, and then open it, and find the showLog() function. Replace it with this:
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 != 'cli') { $summary = "{$this->_queriesCnt} {$text} took {$this->_queriesTime} ms"; $body = array(); $body[] = array("Nr", "Query", "Error", "Affected", "Num. rows", "Took (ms)"); foreach ($log as $k => $i) { $body[] = array(($k + 1), $i['query'], $i['error'], $i['affected'], $i['numRows'], $i['took']); } fb(array($summary, $body), FirePHP::TABLE); } else { foreach ($log as $k => $i) { print (($k + 1) . ". {$i['query']} {$i['error']}\n"); } } }
Then add the following to your app/config/bootstrap.php:
require_once('FirePHPCore/FirePHP.class.php'); function fb() { $debug = Configure::read('debug'); if ($debug) { $ob_setting = ini_get('output_buffering'); if (!$ob_setting) { ob_start(); } $instance = FirePHP::getInstance(true); $args = func_get_args(); return call_user_func_array(array($instance, 'fb'), $args); } else { return true; } }
Finally, create a new component: app/controllers/components/fire_p_h_p.php (I named the file like this, so that the name in code is FirePHP, which I think looks better than FirePhp. Php has always looked wrong to me.) with these contents:
class FirePHPComponent { private $instance; public function __construct() { $ob_setting = ini_get('output_buffering'); if (!$ob_setting) { ob_start(); } $this->instance = FirePHP::getInstance(true); $this->instance->setEnabled(Configure::read('debug')); } public function __call($name, $args) { return call_user_func_array(array($this->instance, $name), $args); } }
Now you can access FirePHP by adding the FirePHP component to your controller, and calling the regulare FirePHP methods, like this:
class FirephpController extends AppController { var $components = array('FirePHP'); public function debug() { $this->FirePHP->error('some error'); } }
My thanks to Tobias Funke and Marwin at the Bakery for the article and comments that helped me get this all set up. It works beautifully, and will greatly improve my productivity going forward.









where to put FirePHP.class.php ??? Following your tutorial I only get an error that require can not find the file. OK, I put it somewhere it can be found but then I only get
Call to undefined method FirePHP::setEnabled() in /path_to_cake_/controllers/components\fire_p_h_p.php
If you could start your utorial with all the necessary instructions howto setup the files, it would be mch easier to follow.
Best IMHO would be to offer a zip file with a working setup.
Thanks!
This is a modification of the guide found here: http://bakery.cakephp.org/articles/view/baking-cakes-with-firephp
If you place the files as they state, with my modifications, it’ll work.
Also, Ogglo, looking at your error, I’m guessing you may not have FirePHP installed, or the incorrect version, or something. I installed FirePHP using the Pear install method.
Thanks for your answer. I followed that tutorial too. So you are NOT using FirePHP Core library for PHP http://www.firephp.org/ ??? But in the tutorial you point to exactly that is mentioned, also if I do a “pear search fire” nothing is found. No firephp in pear.
Would be very cool if you could start over your tutorial from A-Z so that it works if followed. THANKS!
I followed the ‘Standalone’ instructions here:
http://www.firephp.org/HQ/Install.htm
As for updating the tutorial, maybe I’ll do that later today. Thanks for your feedback.
Following these directions ends up with a “Fatal error: Exception thrown without a stack frame in Unknown on line 0″ at the end of the page. A little research suggests this is because you can’t throw an exception in an errorhandler, but that seems to be the only premise of FirePHP.class.php so I’m not sure where to turn to. Have you run into this or have any advice?
very nice! getting the mysql output in firephp makes it even more helpful! thank you.
2altintx:
try to use this in your controller class to avoid this error:
public $components = array(‘FirePHP’);
This article is very useful. Now I can show ANY (not just all) SQL statement. Files you need edit is:
core.php => add this statement: Configure::write(‘debug’,2);
dbo_resoure.php => edit function logQuery
function logQuery($sql) {
if (Configure::read() > 2)
{
…OLD CONTENT
}
}
Wraps any SQL statement you want with:
Configure::write(‘debug’,3);
…SQL statement
Configure::write(‘debug’,2);
I sometimes encounter this error: ““Fatal error: Exception thrown without a stack frame in Unknown on line 0″ I don’t know how to fix it but SQL statement is showed although.