Errorcontroller for each module in ZF1

Asked

Viewed 32 times

1

I have a project that uses Zend Framework 1. It has 2 modules: default and admin.

When a mistake occurs or Exception, Zend directs to Errorcontroller.

The problem is this: when an error occurs inside the module "default", it directs to the module’s Errorcontroller "default" (this is correct). However, when an error occurs in the "admin" module, it also directs to the module’s Errorcontroller "default", causing the error to appear in layout of website, instead of in the layout of the admin.

I own an Errorcontroller in the "admin" module and I thought it was automatic. How do I direct errors from the "admin" module to your Errorcontroller?

1 answer

2


Reference: https://stackoverflow.com/questions/2720037/zend-framework-module-based-error-handling

You can implement a plugin to examine your request and based on the module you are accessing it arrow the specific 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 the example below to register the plugin on your frontController

$front = Zend_Controller_Front::getInstance();
$front -> registerPlugin(new My_Controller_Plugin_ErrorControllerSwitcher())
  • 1

    I had looked in the Soen and did not find. It worked right. Thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.