11

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.

2 Answers 2

23

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.

Sign up to request clarification or add additional context in comments.

6 Comments

You may use $front->hasPlugin($name);
+1 works like a charm and for anyone wondering where to instantiate the plugin. putting Zend_Controller_Front::registerPlugin(new My_Controller_Plugin_Utilities()) in your bootstrap will do it.
Please note I've updated the code (changed the classname), so that Copy&Paste would make more sense. It was part of my utilities class and now it should be more descriptive.
Also fixed minor bugs like the CAPS "http" and fixed the code so that it follow ZF coding standards.
call to registerPlugin didn't work for me. Following in Bootstrap.php does work in my application: protected function _initErrorController() { $this->bootstrap('layout'); $this->bootstrap('frontController'); $layout = $this->getResource('layout'); $front = $this->getResource('frontController'); $front->registerPlugin(new My_Controller_Plugin_ErrorControllerSwitcher()); }
|
3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.