How to disable error controller for a specific module

I have an Api module where I am trying to implement a RESTful API. The problem is that when I make an exception in this module, I would like the exception to be thrown and not handled by the error controller in the default module.

Is it possible to disable the error controller only for a specific module in the Zend Framework?

+4
source share
1 answer

Using the following method, you can disable the error handler for a specific module. In this example, I will name your module RESTful rest .

First create a new plugin in your application. For an example it will be Application_Plugin_RestErrorHandler . Add the following code to application/plugins/RestErrorHandler.php

 class Application_Plugin_RestErrorHandler extends Zend_Controller_Plugin_Abstract { public function preDispatch(Zend_Controller_Request_Abstract $request) { $module = $request->getModuleName(); // don't run this plugin unless we are in the rest module if ($module != 'rest') return ; // disable the error handler, this has to be done prior to dispatch() Zend_Controller_Front::getInstance()->setParam('noErrorHandler', true); } } 

Then, in the Bootstrap module for the rest module, we register the plugin. This is located in modules/rest/Bootstrap.php . Since all modular bootstraps run independently of the current module, it can get into your main boot file, but I like to register plugins associated with a specific module in this modular bootstrap.

 protected function _initPlugins() { $bootstrap = $this->getApplication(); $bootstrap->bootstrap('frontcontroller'); $front = $bootstrap->getResource('frontcontroller'); // register the plugin $front->registerPlugin(new Application_Plugin_RestErrorHandler()); } 

Another possibility is to save the error handler, but use a module-specific error handler. Thus, the error handler for your rest module can behave differently and produce a friendly REST error.

To do this, copy ErrorController.php to modules/rest/controllers/ErrorController.php and rename the class to Rest_ErrorController . Then copy the script view for the error controller to modules/rest/views/scripts/error/error.phtml .

Customize error.phtml to your liking so that the error message uses the same JSON / XML format used by your rest module.

Then we will make a little tweak for the plugin above. What we will do is tell Zend_Controller_Front to use ErrorController :: errorAction from the rest module instead of the default module. If you would like, you could use a different controller than ErrorController. Modify the plugin to look like this:

 class Application_Plugin_RestErrorHandler extends Zend_Controller_Plugin_Abstract { public function preDispatch(Zend_Controller_Request_Abstract $request) { $module = $request->getModuleName(); if ($module != 'rest') return ; $errorHandler = Zend_Controller_Front::getInstance() ->getPlugin('Zend_Controller_Plugin_ErrorHandler'); // change the error handler being used from the one in the default module, to the one in the rest module $errorHandler->setErrorHandlerModule($module); } } 

Using the above method, you still need to register the plugin in Bootstrap.

Hope this helps.

+4
source

Source: https://habr.com/ru/post/1394715/