Zend framework 基于Zend框架模块的错误处理

Zend framework 基于Zend框架模块的错误处理,zend-framework,Zend Framework,Zend_Controller_Plugin_ErrorHandler始终转发到默认模块中的ErrorController::errorAction(),但我希望它具有模块意识。例如,当异常抛出时,必须调用模块的ErrorController,如Admin\u ErrorController:errorAction 我该怎么做? 谢谢。您可以创建插件,该插件将检查您的请求并基于当前模块集ErrorController <?php class My_Controller_Plugin_Err

Zend_Controller_Plugin_ErrorHandler始终转发到默认模块中的ErrorController::errorAction(),但我希望它具有模块意识。例如,当异常抛出时,必须调用模块的ErrorController,如Admin\u ErrorController:errorAction

我该怎么做?
谢谢。

您可以创建插件,该插件将检查您的请求并基于当前模块集ErrorController

<?php
class My_Controller_Plugin_ErrorControllerSwitcher extends Zend_Controller_Plugin_Abstract
{
    public function routeShutdown (Zend_Controller_Request_Abstract $request)
    {
        $front = Zend_Controller_Front::getInstance();
        if (!($front->getPlugin('Zend_Controller_Plugin_ErrorHandler') instanceof Zend_Controller_Plugin_ErrorHandler)) {
            return;
        }
        $error = $front->getPlugin('Zend_Controller_Plugin_ErrorHandler');
        $testRequest = new Zend_Controller_Request_Http();
        $testRequest->setModuleName($request->getModuleName())
                    ->setControllerName($error->getErrorHandlerController())
                    ->setActionName($error->getErrorHandlerAction());
        if ($front->getDispatcher()->isDispatchable($testRequest)) {
            $error->setErrorHandlerModule($request->getModuleName());
        }
    }
}

向FrontController注册插件。谢谢你指出这一点

另一种方法可能是为每个模块(或您需要的功能,例如
Mymodule\u MyException
)抛出特定异常,然后在
Default\u ErrorController

中处理它们。您可以使用$front->hasPlugin($name)+1就像一个符咒,对于任何想知道在哪里实例化插件的人来说。将
Zend\u Controller\u Front::registerPlugin(新的My\u Controller\u Plugin\u Utilities())
放在引导程序中即可。请注意,我已更新了代码(更改了类名),以便复制和粘贴更有意义。它是我的utilities类的一部分,现在应该更具描述性。它还修复了CAPS“http”等小错误,并修复了代码,使其符合ZF编码标准。调用registerPlugin对我来说不起作用。Bootstrap.php中的以下内容在我的应用程序中确实有效:
受保护的函数_initErrorController(){$this->Bootstrap('layout');$this->Bootstrap('frontController');$layout=$this->getResource('frontController');$front->registerPlugin(新My_Controller_Plugin_ErrorControllerSwitcher());}
$front = Zend_Controller_Front::getInstance();
$front -> registerPlugin(new My_Controller_Plugin_ErrorControllerSwitcher())