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 ErrorController like Admin_ErrorController:errorAction.

How can i do this? Thanks.

Answers


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.


Need Your Help

Empty Git submodule folder when repo cloned

git version-control git-submodules git-clone

I have one repo hosted at https://github.com/aikiframework/json . On my local copy, I added a submodule using the command