Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 943
cnkt Avatar asked Apr 27 '10 09:04

cnkt


2 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.

like image 110
Tomáš Fejfar Avatar answered Nov 17 '22 10:11

Tomáš Fejfar


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.

like image 25
takeshin Avatar answered Nov 17 '22 08:11

takeshin