I have a module Api where I am trying to implement a RESTful API. The problem is that when I throw an exception in that 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 for just a specific module in Zend Framework?
Using the following method you could disable the error handler for a specific module. In this example, I will call your RESTful module
rest.First, create a new plugin in your application. For the example, this will be
Application_Plugin_RestErrorHandler. Add the following code toapplication/plugins/RestErrorHandler.phpNext, in your module Bootstrap for the
restmodule, we will register the plugin. This is inmodules/rest/Bootstrap.php. Since all module bootstraps are executed regardless of the current module, it could go in your main bootstrap, but I like to register plugins related to a specific module in that module’s bootstrap.Another possibility would be to keep the error handler, but use a module specific error handler. This way, the error handler for your
restmodule could behave differently and output a REST friendly error.To do that, copy
ErrorController.phptomodules/rest/controllers/ErrorController.phpand rename the class toRest_ErrorController. Next, copy the view script for the error controller tomodules/rest/views/scripts/error/error.phtml.Customize error.phtml to your liking so the error message uses the same JSON/XML format used by your rest module.
Then, we will make a slight tweak to the plugin above. What we will do is tell Zend_Controller_Front to use ErrorController::errorAction from the
restmodule instead of the default module. If you wanted, you could even use a different controller than ErrorController. Change the plugin to look like the following:With the above method, you still need to register the plugin in Bootstrap.
Hope that helps.