I am currently working on an MVC application that implements the front-controller pattern.
The process works like this:
- request is received
- request gets turned into a RequestObject
- RequestObject is passed to the dispatcher
- Dispatcher then routes and then calls the required controller.
- Controller returns the results in a ResponseObject.
- Dispatcher then returns the ResponseObject to the "Front" of the application.
- The output is then echoed out.
There is a special case (for ajax and flash frontends):
- The dispatcher will see that the request is sent to the "endpoint" controller.
- The endpoint controller then dispatches the request to the actual requested controller as above.
- A requestObject is then returned to the endpoint controller.
- Endpoint controller then does its JSON or AMF encoding and this is echoed.
- The script then terminates with
exit()
;
I have created a ExceptionHandler
class and registered it with set_exception_handler
. At the same time I have created an ErrorHandler
and using set_error_handler
, all errors are then converted and thrown as exceptions.
All of this works well. I am however struggling with the problem when ExceptionHandler
catches one of these exceptions. In the ExceptionHandler
, the ResponseObject is modified to reflect the fact that we need to throw a 500 error.
I would like to somehow return the ResponseObject from the ExceptionHandler
so the returned ResponseObject can be "caught" by the dispatcher and then rendered or transformed into a JSON or AMF response by the "endpoint" controller or the "front" controller.
Is this possible? If so, what's the best way to do this?