I have implemented a structure in order to handle errors and exceptions.
I have an Mvc application.
I have a class that handles all errors and exceptions, set using
This class chooses when and how it has to display the error to the user. Specifically, when an exception occurs in production environment, I want to show a generic error page to the user and log the error details (for example in the database) for further investigations.
In order to do so, after having logged the error details, the exceptions handler class has access to the Dispatcher object invoking the following command:
$this->dispatcher->forward(array( ... )); $this->dispatcher->dispatch();
array() I specify the Controller and the Action where the generic error page will be rendered. Then, I NEED to call
->dispatch() otherwise the forward wouldn't work.
That action contains something like that:
$this->response->setStatusCode( 500, 'Internal Server Error' ); $this->setPageDefaultTitle( 'Ooops! Something went wrong here...' ); $this->setPageDefaultBody( 'Dear user, unfortunately an error occured. We\'re already working on it!' );
Ok, in my
ControllerBase (extended by all my Controllers) I also have a
afterExecuteRoute method that does some logic before sending the control to the view.
The problem is that when an error occurs everything works perfectly excepts that the view is totally not rendered. I see a blank page and no errors in Nginx log.
If, at the end of
afterExecuteRoute method I invoke these commands:
$this->response->send(); $this->view->render( $this->dispatcher->getControllerName(), $this->dispatcher->getActionName() );
Everything works and the error page is rendered.
I can't understand why - and I also wouldn't like to execute those lines of code, because the forward works, the view is active and working... There's no reason for the view not to render.
Thank you very much.