Utilize FireBug (and FirePHP) to speed up Magento development

In: Magento| PHP| Zend Framework

4 Jan 2009

Upsss… it’s 1 hour after midnight, still not sleeping. Hopefully I’ll manage to wake up in the morning, since I’m working. Anyhow… here’s a little something I made for you guys (for those who find it useful).

I was playing around with Zend Framework today, checking out available classes and methods. My eyes filled with yoj when I came across Zend_Wildfire_Plugin_FirePhp class. It’s located under /Zend/Wildfire/Plugin/FirePhp.php folder in ZendFramework. It’s around 600 hundred lines or so. What’s cool about this class is that it has this cool method you can use to shoot out messages to FireBug. If my info is correct, this class is added to ZendFramework from version 1.6. As of time of this writing, latest version of Magento is 1.2.0.1 and it comes packed with ZendFramework 1.7.2 which is also the latest version as of time of this writing. So, there you go, bingo. We now have this cool little class that can save as a bunch of time (or not).

My point is, the PHP debugging solution generally suck. I know, it’s a hard word, and I like to avoid statements like that as much as I can, but sometimes the feeling just take you over :)

I wrote a little function that you can use in Magento to output messages to FireBug. This, off course, is just my version of doing things. Feel free to implement some different solution if you find my lacking in some aspect.

Here is my function

/**
*
* @author Branko Ajzele
* @license GPL
*
* @param object $var Object we wish to debug in FireBug
* @param string $label Nice little label we wish to add so we can see it in FireBug
* @param string $style String holding one of values: ‘LOG’, ‘INFO’, ‘WARN’, ‘ERROR’, ‘TRACE’, ‘EXCEPTION’, ‘TABLE’
* @return void Return null if Mage::setIsDeveloperMode(false) otherwise return message to browser
*/
function acl($var, $label, $style = ‘LOG’)
{
if(Mage::getIsDeveloperMode())
{
$request = new Zend_Controller_Request_Http();
$response = new Zend_Controller_Response_Http();
$channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
$channel->setRequest($request);
$channel->setResponse($response);

/*
* Start output buffering
*/
ob_start();

Zend_Wildfire_Plugin_FirePhp::send($var, $label, $style);

/*
Style     Description
LOG     Displays a plain log message
INFO     Displays an info log message
WARN     Displays a warning log message
ERROR     Displays an error log message that increments Firebug’s error count
TRACE     Displays a log message with an expandable stack trace
EXCEPTION     Displays an error long message with an expandable stack trace
TABLE     Displays a log message with an expandable table

*/

/*
* Flush log data to browser
*/
$channel->flush();
$response->sendHeaders();
}
else
{
return null;
}
}

All you need to do is to place it inside the index.php file, the one inside the root Magento folder. You need to place it after Mage::setIsDeveloperMode(true); statement. Which by the way you need to uncomment (in development environment). That’s it, your done.

Now all you need to do is go into one of your template files like /catalog/product/view.phtml and call this function like

acl($this->debug(), ‘Product view page aka view.phtml VAR: $this’);
or
acl($_product->debug(), ‘Product view page aka view.phtml VAR: $_product’);

I provided a screenshot below for you to see the result of the two function calls.

magentodebugwith-zend_wildfire_plugin_firephp

And here is the full version of my index.php file.

To summarize. For this to work you need

  • Firefox with installed FireBug and FirePHP
  • Copy - Paste my function from above to your index.php file
  • Just call acl() function with appropriate params inside one of your template files

Although this may not be the “happiest” soultion, since some messages get duplicated in FireBug if you call acl function from controllers, hope some of you find this useful.

Interesting? Share it!
  • Digg
  • del.icio.us
  • Google
  • Technorati
  • Facebook
  • TwitThis
  • description

16 Responses to Utilize FireBug (and FirePHP) to speed up Magento development

Avatar

Frank

January 4th, 2009 at 9:05 pm

This is a great find. Seems like it would be best to work this into an extension of some kind which would allow you to enable various debug statements based on an administrative set of check boxes.

Avatar

Tony

January 5th, 2009 at 2:22 am

I noticed in your screenshot that you don’t have the zend studio firefox debugger extension installed. Have you tried that out?

http://www.zend.com/en/products/studio/downloads

select “studio web debugger”

T

Avatar

Tony

January 5th, 2009 at 2:24 am

@Tony

sorry, it’s “Studio Browser Toolbar” then select “cross platform firefox” or “IE for windows”.

Avatar

branko

January 5th, 2009 at 2:32 am

Hi Tony,

Thnx for the tip. It’s just that I work across various computers, and the only thing they have in common is Firefox :) so the above approach seems satisfying.

I’ll be sure to test out the Zend debugg toolbar… It’s already installed, I just hide the toolbar :)

Avatar

Tony

January 5th, 2009 at 11:36 am

For sure, this is a great post. I haven’t used the zend debugger toolbar either. I’m following your series on zend studio and before that netbeans. I’m on the same computer all day everday. What distro is that you’re running btw?

Avatar

branko

January 5th, 2009 at 1:29 pm

It’s latest openSuse 11.1… although I see no big significant difference between other distros. It’s mostly a showoff for friends… KDE 4 looks quite candy :)

Avatar

Manos

January 19th, 2009 at 6:20 am

I’d just like to say thanks for this article. Even though it’s not my first day with PHP and I already have FireBug, I had no idea the whole FirePHP thing existed. This is a lifesaver with magento where the old-school “echo” debug method just isn’t gonna cut it.

Avatar

Riki Risnandar

March 13th, 2009 at 4:12 pm

nice tricks !! thanks for sharing this.. i was using the var_dump to output the var to the browser but sometimes the magento application is stuck

Avatar

Nightfly

March 16th, 2009 at 2:43 pm

There is also PHPEdit, which has a PHP debugger and a Firefox extension.

Avatar

branko

March 16th, 2009 at 2:52 pm

Thnx for the input Nightfly.

I must say I get chills when someone mentions PHP and integrated debugger in same sentence.

Good luck using any IDE’s debugger with Magento.

I’m really sorry if I sound a bit sarcastic, it’s just that I lost countless hours on trying to setup NetBeans and ZendStudio to work with the XDebug. All I got was my Apache crashing on every second page load, and the depth of debugging was hilarious. Every more complex classes and functions caused failure of debugger.

I finally end up using FireBug and var_dump combinations.

I would really appreciate if you can share some link or article that describes in detail the process of debugging platforms like Magento.

Thank you.

Avatar

Nightfly

March 17th, 2009 at 2:05 pm

In the beginning PHP debugging was a nightmare, but with modern PHPEdit http://www.phpedit.com/ its Firefox extension http://www.waterproof.fr/blog/index.php?/archives/27-New-firefox-extension.html and XAMPP it’s a breeze.

I basically install XAMPP, enable xdebug with default settings in the php.ini, disable Zend extensions (Loader etc.), install PHPEdit and the Firefox extension, create a project, set a breakpoint eg. in index.php, tick the “enable debugging” button in Firefox, load the Page and a few seconds later the PHPEdit debug session has started. :)

Also, thanks for the article, FirePHP is really a great help for Magento development! I’m just coding a backend module and it helps a lot to debug models and so on.

Avatar

Nightfly

March 17th, 2009 at 2:07 pm

BTW, XAMPP 1.7.0 doesn’t work with Magento (MySQL PDO is broken) unless you fix it up with the solution presented here: http://www.apachefriends.org/f/viewtopic.php?f=16&t=32617

Avatar

NIRMESH

April 7th, 2009 at 7:07 am

Hi branko,,

i wanted to know why after enabling log from admin panel also i m unable to find any log file in basedir\var\

is there any other setting i need to do???

Avatar

Scott

April 13th, 2009 at 4:13 pm

There’s a debug module here: http://www.magentocommerce.com/extension/reviews/module/949/debug-module

…but I’ve yet to be able to get it to install. Hopefully it will be working soon though, without the need for a workaround at least.

Avatar

Scott

April 13th, 2009 at 4:41 pm

as a follow up, use this key and it works fine (even though it didn’t work with the dropdown set to beta):

magento-community/Netresearch_Debug-beta

Avatar

Nightfly

June 30th, 2009 at 3:06 am

BTW I just tried Firefox 3.5, which has the new Tracemonkey optimizing JS engine. With javascript.options.jit.chrome option set to true, even large FirePHP logs (such as almost any Magento class) can now be handled without waiting half an hour on FirePHP or Firefox crashing.

Comment Form

About this site

Branko Ajzele Welcome to ActiveCodeline. My name is Branko Ajzele. I'm 26-year-old, currently working as full time PHP web application developer at Inchoo (in partnership with Surgeworks). My special interest and expertise evolve mainly around Zend Framework, Doctrine ORM, Magento eCommerce platform and few other related technologies.

More about me?
View Branko Ajzele's profile on LinkedIn
  • Uwe Stoll: Thanks for your valuable advice. Furthermore, I found it be important to use the backend generated [...]
  • Danny: Ah. I think I figured it out.. But 1 thing.. How can I have it appear at just the home page and on t [...]
  • Danny: I've set everything up as instructed on this page, but it still doesn't show up on the front page... [...]
  • Chris: [code] true local [/code] [...]
  • Chris: Looks like the author forgot to include a pool to which the code comes from. local or community. [...]

Categories