You are here:Home arrow Home arrow PHP arrow Debugging your ZF application with Firebug
  • Narrow screen resolution
  • Wide screen resolution
  • Decrease font size
  • Default font size
  • Increase font size
  • default color
  • green color
  • blue color

Debugging your ZF application with Firebug

Sunday, 08 February 2009
Debugging your application using Firebug can be a handy substitute for using Zend_Debug::dump (). In this tutorial we are going to show you, how you can create an action helper for dumping your php variables to Firebug's console.
Our goal is getting ability of printing debug messages, as well as dumping php variables onto Firebug's console. For people who still don't know, Firebug is a great addon for Firefox:

Image

So get the Firefox browser (you don't have Firefox, huh?) and install Firebug addon. Next thing to do is installing Firephp addon. This addon allows web pages to send messages to Firebug's console. Once installed, make sure to allow Firebug and Firephp for your Zend Framework application website. Next, create the following directory structure under library/ :  library /
   - Zend
   - Smartycode
     - Controller
       - Action
         - Helper
           - Logger.php
Add this line to your bootstrap file: // register the default action helpers
Zend_Controller_Action_HelperBroker::addPath($rootPath.
   '/library/Smartycode/Controller/Action/Helper',
   'Smartycode_Controller_Action_Helper');
This registers our logger helper with the helper broker. You can put the Logger.php somewhere else and change the suggested folder structure, but you will have to change the prefix path in call to addPath (). Here are the contents of the view helper. Simply copy the lines below into Logger.php <?php
/**
 * Logger helper class
 * @author Danila V. (www.smartycode.com)
 * @version $Id: Logger.php 15703 2009-02-06 22:56:49Z danila $
 *
 */

class Smartycode_Controller_Action_Helper_Logger
   extends Zend_Controller_Action_Helper_Abstract
{
  /**
   * Logger instance
   *
   * @var Zend_Log The logger
   */

    private $logger;
   
    /**
     * Constructor: initialize plugin loader with logger instance
     * depending on if configuration allows it
     * @return void
     */

    public function __construct()
    {
      if(Zend_Registry::isRegistered('logger')) $this->logger = Zend_Registry::get('logger');
    }
   
    function direct($message, $priority)
    {
        if ($this->logger) $this->logger->log($message, $priority);
    }
   
  function __call($name,$arguments) {
   
    if ($this->logger) {
      // Proxy over to existing methods of Zend_Log as well
                        // as the shortcut methods (by __call in Zend_Log)
      call_user_func_array(array($this->_logger, $name), $arguments );
    }
  }    
}
As you can see, the helper proxies to Zend_Log methods, thus any methods of Zend_Log are available to it. Now onto the final part. You should create the logger itself. Head on to your bootstrap file and add: //Enable log
if ('development' == APPLICATION_ENVIRONMENT) {
  $logger      = new Zend_Log();
  $text_writer = new Zend_Log_Writer_Firebug();
    $logger->addWriter($text_writer);
    Zend_Registry::set('logger', $logger);
}
You surely will want to modify the lines that check the development environment. Needless to say it's important to turn off the Firebug logging on production environment. Let's see some usage examples now: Open up any controller code and try these: // direct method of helper proxies to log()
$this->_helper->logger($someVarOrObject, Zend_Log::INFO);
// shortcut methods of Zend_Log are available as well
$this->_helper->logger->err('Some error text');
Questions? Comments? Send them to master at smartycode dot com
Happy debugging!




Reddit!Del.icio.us!Facebook!Slashdot!Netscape!Technorati!StumbleUpon!
Last Updated ( Sunday, 29 March 2009 )

Add comment


Security code
Refresh

< Prev