1
votes

Is there a way, instead of showing the template 404 page, to have an event, and when it doesn't find an controller, or module, to do an action, like a redirect?

1

1 Answers

2
votes

In the onBootstrap method in your Module.php you can attach a function to execute when an event occurs, the following attach a function to be executed when an error (exception) is raised:

public function onBootstrap(MvcEvent $e)
{
    $application = $e->getApplication();
    $em = $application->getEventManager();
    //handle the dispatch error (exception) 
    $em->attach(\Zend\Mvc\MvcEvent::EVENT_DISPATCH_ERROR, array($this, 'errorHandler'));
    //handle the view render error (exception) 
    $em->attach(\Zend\Mvc\MvcEvent::EVENT_RENDER_ERROR, array($this, 'errorHandler'));
}

and then define the function to handle the error in any way you want, the following is an example:

public function handleError(MvcEvent $e)
{
    //get the exception
    $exception = $e->getParam('exception');
    //...handle the exception... maybe log it and redirect to another page, 
    //or send an email that an exception occurred...
}