如何通过application.ini设置ErrorHadler?

发布于 2024-10-07 02:29:42 字数 514 浏览 0 评论 0原文

我这样在 Bootstrap.php 中设置了默认的 errorHandler

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 选项进行相同操作?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽心 2024-10-14 02:29:42

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

但是,如果您想引导自己的个人错误处理程序,您可以执行以下操作:

在 application.ini 中:

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

并且在引导程序中加载这些选项:

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

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文