Search code examples
zend-frameworkconfigurationconfigzend-application

How to set up ErrorHadler via application.ini?


I set up default errorHandler in Bootstrap.php this way:

public function _initErrorHandler()
{
    $frontController = Zend_Controller_Front::getInstance();
    $plugin = new Zend_Controller_Plugin_ErrorHandler(
        array(
            'module' => 'default',
            'controller' => 'error',
            'action' => 'error'
    ));
    $frontController->registerPlugin($plugin);

    return $plugin;
}

How can I make the same via application.ini options?


Solution

  • If you mean "automatically" I don't think it's possible, since the ErrorHandler plugin isn't a resource plugin.

    But, if you want to bootstrap your own personal error handler, you can do something like this:

    in your application.ini:

    errorhandler.class = "Zend_Controller_Plugin_ErrorHandler"
    errorhandler.options.module = default
    errorhandler.options.controller = error
    errorhandler.options.action = error 
    

    And, in your bootstrap to load these options:

    public function _initErrorHandler()
    {
        // make sure the frontcontroller has been setup
        $this->bootstrap('frontcontroller');
        $frontController = $this->getResource('frontcontroller');
        // option from the config file
        $pluginOptions   = $this->getOption('errorhandler');
        $className       = $pluginOptions['class'];
    
        // I'm using zend_loader::loadClass() so it will throw exception if the class is invalid.
        try {
            Zend_Loader::loadClass($className);
            $plugin          = new $className($pluginOptions['options']);
            $frontController->registerPlugin($plugin);
            return $plugin;
        } catch (Exception $e) {
            // do something useful here (like fall back to the default error handler)
            echo $e->getMessage();
            return null;
        }
    }