如何通过application.ini设置ErrorHadler?

时间:2010-12-12 14:48:12

标签: zend-framework configuration config zend-application

我以errorHandler这样设置默认Bootstrap.php

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;
}

如何通过application.ini选项进行相同操作?

1 个答案:

答案 0 :(得分:1)

如果您的意思是“自动”我认为不可能,因为ErrorHandler插件不是资源插件。

但是,如果你想引导你自己的个人错误处理程序,你可以这样做:

你的application.ini中的

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

并且,在你的bootstrap中加载这些选项:

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;
    }
}
相关问题