Zend Framework Module Based Error Handling
Zend_Controller_Plugin_ErrorHandler always forwards to ErrorController::errorAction() in the default module but i want it be module aware. For example when a exception throws it must be call the module's Err开发者_如何学JAVAorController like Admin_ErrorController:errorAction.
How can i do this? Thanks.
You can create plugin that will examine your request and based on current module sets 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());
}
}
}
Then use
$front = Zend_Controller_Front::getInstance();
$front -> registerPlugin(new My_Controller_Plugin_ErrorControllerSwitcher())
to register the plugin with the FrontController. Thanks JohnP for pointing that out.
Alternate approach may be to throw specific exceptions for each module (or functionality you need, e.g. Mymodule_MyException
) and then handle them in the Default_ErrorController
.
精彩评论