Darren O'Neill

Integrate Bugsnag into ZF2

In Zend Framework 2 you approach exception handling quite differently from how you currently do in version 1 of the framework. Gone are error controllers and in are event listeners.

If you like to use an an external service to notify you of raised exceptions you will need to rework any old code. Below is an example of integrating Bugsnag into ZF2 using Composer.

Firstly update your composer.json file to include the PHP Bugsnag notifier:

    "require": {
        "bugsnag/notifier": "1.0.0"

Now add the following to your Application Module.php file:


use Bugsnag;

public function onBootstrap($e)
    $eventManager = $e->getApplication()->getEventManager();

        'dispatch.error', function($event) {
            $error = $event->getError();

            $sm     = $event->getApplication()->getServiceManager();
            $config = $sm->get('application')->getConfig();

            $environment = $config['environment'];

            if (($error == 'error-exception') && ($environment == 'production')) {
                $exception = $event->getParam('exception');



This assumes you have a config variable (in your local.php file) called environment. If it is set to production exceptions will be passed to Bugsnag.

Note: this will only send uncaught exceptions to Bugsnag. If you would like to send all exceptions you should take the above code, create a service and then use this throughout your application as required. It is also advised to add your API key as a config variable rather than hardcoding it in this file.