-- Ralf Kramer <[EMAIL PROTECTED]> wrote
(on Thursday, 13 September 2007, 02:11 AM +0200):
> I'd like to develop a module based application and want to assign an own
> ErrorHandler class for each module. So I have a structure like this:
>
> /public
> /controllers
> IndexController.php
> ErrorController.php
> /admin
> /controllers
> IndexController.php
> ErrorController.php
> /user
> /controllers
> IndexController.php
> ErrorController.php
>
> The entire Applciation uses one very simple bootstrap:
> $frontController = Zend_Controller_Front::getInstance();
> $frontController->setControllerDirectory(array(
> '' => '../application/public/controllers/',
> 'admin' => '../application/admin/controllers/',
> 'user' => '../application/user/controllers/'
> ));
> $frontController->returnResponse(true);
> $response = $frontController->dispatch();
> $response->sendResponse();
>
>
> I know that I can change the default forward to
> ErrorController::errorAction() by calling
> Zend_Controller_Plugin_ErrorHandler::setErrorHandlerModule()
> but this alone wont help. How can I assign the ErrorHandler dynamically
> accordingly to the currently routed module? What is best practice in
> this case. Any help and hints are welcome.
First off, you can grab the error handler plugin from the front
controller, and modify it once retrieved:
$errorHandler = Zend_Controller_Front::getInstance()
->getPlugin('Zend_Controller_Plugin_ErrorHandler');
$errorHandler->setErrorHandlerModule(...);
If you want to have it dynamically change per-action, the easiest way
would be to create a plugin that runs a preDispatch(); it would look
something like this:
class OverrideErrorModule extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$module = $request->getModuleName();
$errorHandler = Zend_Controller_Front::getInstance()
->getPlugin('Zend_Controller_Plugin_ErrorHandler');
$errorHandler->setErrorHandlerModule($module));
}
}
and then make sure to register this in your bootstrap:
$front->registerPlugin(new OverrideErrorModule());
--
Matthew Weier O'Phinney
PHP Developer | [EMAIL PROTECTED]
Zend - The PHP Company | http://www.zend.com/