TAAT Technologie Cyfrowe

Zend Framework Tutorial

Debugging and database profiling with FirePHP

FirePHP is very useful FireBug extension and since Zend Framework v.1.7 is natively supported.

All you need is to install FireBug and FirePHP Firefox extensions and configure logging in your Bootstrap.php.

Doctrine database profiling with FirePHP requires additonal class /library/Imind/Profiler/Doctrine/Firebug.php which can be downloaded from Imind-PHP project page.

The Bootstrap

In your Bootstrap.php add setupFirePHP() method:

/**
 * setup FirePHP
 * @access public static
 * @param bool $enable Enable or disable logging
 * @return null
 */
public static function setupFirePHP($enabled = true) {
    $writer = new Zend_Log_Writer_Firebug();
    $logger = new Zend_Log($writer);
    // add FirePHP logger to the registry
    Zend_Registry::set('logger', $logger);

    if ($enabled) {
        $writer->setEnabled($enabled);
        // setup Doctrine profiling
        $profiler = new Imind_Profiler_Doctrine_Firebug('All DB Queries: ');
        Doctrine_Manager::connection()->setListener($profiler);
        ob_start();
    }
}

To start logging and database profiling, add one line to setupDatabase() method:

/**
 * Setup Database
 * @access public static
 * @return null
 */
public static function setupDatabase() {
    My_Controller_Plugin_Doctrine::setupDoctrine();
    self::setupFirePHP(Zend_Registry::get('config')->debug);
    My_Controller_Plugin_Doctrine::setupSession();

    // or if you don't need database in Bootstrap
    // self::$frontController->registerPlugin(new My_Controller_Plugin_Doctrine());
    }

To display all profiling result in FireBug console, add one line at the end of sendResponse() method:

// Display Doctrine Profiling information in Firebug
Zend_Wildfire_Channel_HttpHeaders::getInstance()->flush();

How do we use it?

FirePHP is enabled/disabled via global debug settings in .htaccess, or via $enabled boolean parameter of setupFirePHP() method.

To display info message in FireBug console, use Zend_Registry::get('logger')->err('My test error message');, Zend_Registry::get('logger')->log($myarray); and so on. Check FirePHP Headquarters for more information.

Let’s simplify this notation by adding fb() function (not method!) at the end of the Bootstrap.php (after ending bracket of the Bootstrap class):

/**
 * FirePHP debugging shortcut
 */
function fb($message, $label = null)
{
    $logger = Zend_Registry::get('logger');
    if ($label != null) {
        $logger->log($label . ':', Zend_Log::INFO);
    }
    $logger->log($message, Zend_Log::DEBUG);
}

Now we can use FirePHP debugging like this: fb($myvar) or fb($myvar, 'My label');

Additional reading

Downloads