FirePHP and Zend Framework 1.6
September 2, 2008

The long awaited day has arrived. Zend Framework 1.6 just shipped and with it native support for FirePHP!

This means that if you use Zend Framework (ZF) you can now log to your Firebug Console via FirePHP without needing to download any other PHP files such as the FirePHPCore library.

There are two primary components available at this time. One is the Zend_Log_Writer_Firebug and the other the Zend_Db_Profiler_Firebug.

Lets check out how to use these new components. I would think that most ZF applications use Zend_Controller_Front so the following examples assume you have a basic MVC application.

Install Firebug and FirePHP

You first need to install the Firebug and FirePHP Firefox extensions. You can install them from here:

Logging to FirePHP with Zend_Log

All logging in ZF is done via Zend_Log which delegates the actual logging to registered log writers. The Zend_Log_Writer_Firebug component is such a writer and as the name indicates it sends your log data to Firebug.

Place the following code in your bootstrap file somewhere before dispatching your front controller:

$logger = new Zend_Log();
$writer = new Zend_Log_Writer_Firebug();
$logger->addWriter($writer);

To keep an instance of your logger you can use:

Zend_Registry::set('logger',$logger);

The Firebug log writer is all setup and ready to go. You can now dispatch your front controller and log data from within any of your model, view and controller files with the following:

$logger = Zend_Registry::get('logger');
$logger->log('This is a log message!', Zend_Log::INFO);

If you have done everything correctly you should see “This is a log message!” in your Firebug Console. If it is not showing up, make sure you have the Net and Console Firebug panels enabled.

The Zend_Log::INFO constant passed to log() determines the priority of the logging message. To increase the priority you can use Zend_Log::WARN or Zend_Log::ALERT for example which will produce a log message with a warning and error style respectively.

To log a plain message without any icon in Firebug you can use Zend_Log::DEBUG.

While logging simple strings is fun, FirePHP really comes into its own when logging complex data structures such as multi-dimensional arrays and objects. All you need to do is pass the object you want to log to the log() method, specify the logging priority and FirePHP will render the data appropriately.

For example you can log your exceptions with a complete expandable stack trace with the following code:

try {
    // Code that may throw an exception
} catch(Exception $e) {
    Zend_Registry::get('logger')->err($e)
}

Note the above code also uses the err() shortcut instead of passing the Zend_Log::Err constant to log().

For extensive documentation on how to use the Firebug log writer including information on how to create tables in the Firebug Console please see the online documentation.

Profiling Database Queries

There are numerous reasons for wanting to see a list of database queries executed during a page request. The Zend_Db_Profiler_Firebug component makes this possible and displays the result in an expandable table in the Firebug Console.

Place the following code in your bootstrap file somewhere after your database adapter ($db) is initialized and before dispatching your front controller:

$profiler = new Zend_Db_Profiler_Firebug('All DB Queries');
$profiler->setEnabled(true);
$db->setProfiler($profiler);

That’s it! Any database queries executed in your model, view and controller files will now be sent to Firebug along with the time it took to execute each, the query that was executed and any parameters bound to the query.

For a more detailed tutorial on how to use Zend_Db_Proflier_Firebug see here.

Try it Out!

You may be used to debugging your ZF code using Zend_Debug::dump() or even var_dump(). What do you have to loose trying these new components? They offer the big advantage of not dumping your debug data to the page. They allow you to debug applications with intricate designs as well as AJAX request that require clean JSON or XML responses.

Even if you are used to xDebug, FirePHP may prove to be a faster debugging solution in some cases than starting a debug session every time you need to ge some quick feedback as to what your variables are doing.

If you get tired of typing Zend_Registry::get(‘logger’)->debug(); every time you need to print some debug data you can place the following wrapper function in your bootstrap file:

function fb($message, $label=null)
{
    if ($label!=null) {
        $message = array($label,$message);
    }
    Zend_Registry::get('logger')->debug($message);
}

Now you can simply use:

fb('This is a log message!');
fb($_SERVER, 'Server Variables');

One last note of caution. Do not log your debug data for everyone to see! Make sure you disable all logging when your application runs in production. Instead of removing your logging code every time you upload your application, you can disable the log writer and database profiler in your bootstrap file with:

$writer->setEnabled(false);
$profiler->setEnabled(false);

More Information/Help

For detailed information on these two components please refer to the Zend Framework Manual. Specifically the Zend_Log_Writer_Firebug and Zend_Db_Profiler_Firebug chapters.

If you need help use one of the following forums:

47 Comments
Bruno Friedmann
September 3, 2008

Hi Christoph.

Thanks for this little trick & tips … Very nice and useful.

Just add a more precise line that we need firebug + firephp .. It take me an hour to understand what I’ve missed ;-)
Ok perharps just too early in the morning.

Christoph Dorn
September 3, 2008

I have added an “Install Firebug and FirePHP” section. Thanks for the feedback!

September 3, 2008

Hello Christoph,

I am happy to see FirePHP found its way to Zend Framework. :)

September 3, 2008

Thanks for the tips. The framework integration with Firebug by FirePHP (that is the first time that I heard off – I know shame) in a way that I can log what is going on, this will make my work a lot easier. I Love that.

September 3, 2008

Just few days ago, I was looking Zend_Log_Writer_Firebug and I was very pleased with it. But I didn’t know that Zend_Db_Profiler_Firebug is available 10x for the tips ;)

Christoph Dorn
September 3, 2008

Thanks for the great feedback!

As a side note. Please do not post problem reports or requests for help here. You can get help via the Zend Framework General mailing list or the FirePHP discussion group.

September 3, 2008

I can’t wait to get to the office tomorrow to start replacing those Zend_Debug::dump() statements!

September 3, 2008

Thanks for the writeup on this… I’ve been playing around with it since it was included in the RC’s with varying success. Before Zend I was used to Smarty’s debug output (which flopped greatly for AJAX work). Some quick notes, when you start having problems, check the headers coming down to see if the debug data is coming down – I was not sure where my problem was, but it basically ended up being the client every time. There are some issues with versions of FirePHP / Firebug / Zend, but I think being at the newest of the other two fixes it. So if you are at an older version, upgrade!

But like it says above, give this feature a try and you will never go back.

On the “last note of caution” about debug data for all to see – I’ve done it two ways in the past. Lately I have an ini setting that tells the system it is in a development environment which changes a large amount of behaviours – including the logging. Previously I have also checked the host server to compare the name ~ if its not the live server address then it runs in a development mode (the chance for an error there lead me to the ini way now).

But Christoph has done everyone a valuable service by promoting this here

Christoph Dorn
September 3, 2008

Great points Bill.

Always upgrade to the latest versions of Firebug and FirePHP as well as ZF. There are reasons why updates are released and they don’t just contain new features!

Using an ini setting to turn on debugging is the best approach I think. Make sure the config file is NOT part of your application package and the only way you can get it onto a production server is by hand. That way you never have to worry about disabling debugging before you upload.

And make sure your system always defaults to production mode if no debug ini setting is found. That way you never run the risk of exposing anything if your configuration is wrong for whatever reason.

Leave
September 4, 2008

thank’s

Seb
September 5, 2008

Cheers for the tips, now all I need to figure out is how to add data to the Server tab on Net tab in Firebug.

Christoph Dorn
September 5, 2008

You can use:

Zend_Wildfire_Plugin_FirePhp::send(‘Value’, ‘Key’, ‘DUMP’);

But there was a bug with this functionality which will be fixed in 1.6.1:

http://framework.zend.com/issues/browse/ZF-4203

Jim Pringle
September 5, 2008

Thanks for the tutorial. Really helpful.

William
September 8, 2008

All of this requires the use of Zend Framework’s MVC. The one thing I really like about Zend Framework is that it’s not entirely coupled with it’s MVC. You can use many parts of the framework without using MVC. Is there any way or any plan on making FirePHP work outside of an MCV model?

William
September 8, 2008

Oops, that will teach me not to toroughly look at the doc. Thanks for the good work, sorry for the false assumption.

September 10, 2008

Hello Christoph!

I think your FirePHP Plugin is very helpful especially for debugging ajax requests – thats one of your major purpose of this plugin, i think!

Thank you for your work!

Thomas

Erik
September 11, 2008

I had already read something about this Firebug module in ZF, but I thought I wouldn’t need it because I write unit tests for all my controllers and models/data abstraction classes (etc). But it seems like this will not mess up my code at all, so this will come in handy. Thanks!

doez
September 15, 2008

thnk’s

Joel
September 18, 2008

Thanks this will certainly be of great use to me on future projects.

October 13, 2008

Thanks for this little trick & tips … Very nice and useful.

Hans
December 8, 2008

This is just ridiculously awesome… debugging to headers. I wish I were smarter and thought of these things myself.

December 14, 2008

nice and helpful tips

February 9, 2009

Hi Chris,

Really helpful, I knew that its possible using the zend debugger toolbar, good to know that we can do the same using zend framework.

Thanks a lot.

March 12, 2009

Excellent tutorial. Thank you for putting it together.

April 24, 2009

Thanks a lot for this article. FirePHP really got me going now on my second day programming within Zend Framework 1.7.8.
It works like a charm!
Cheers, Stinie

June 10, 2009

Hey,

This was a super helpful article, and the comments are great too.

Keep up the good work!

Ant

Jochen Weber
June 24, 2009

Thanks for this great tutorial.
For those who just sett up their enviroment: php configuration “output_buffering” must be enabled.
Jochen

Comments are closed.