1

I am currently working on an MVC application that implements the front-controller pattern.

The process works like this:

  1. request is received
  2. request gets turned into a RequestObject
  3. RequestObject is passed to the dispatcher
  4. Dispatcher then routes and then calls the required controller.
  5. Controller returns the results in a ResponseObject.
  6. Dispatcher then returns the ResponseObject to the "Front" of the application.
  7. The output is then echoed out.

There is a special case (for ajax and flash frontends):

  1. The dispatcher will see that the request is sent to the "endpoint" controller.
  2. The endpoint controller then dispatches the request to the actual requested controller as above.
  3. A requestObject is then returned to the endpoint controller.
  4. Endpoint controller then does its JSON or AMF encoding and this is echoed.
  5. 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?

F21
  • 32,163
  • 26
  • 99
  • 170

1 Answers1

0

It isn't possible. As stated on http://php.net/set_exception_handler

Execution will stop after the exception_handler is called.

The right way is to add try-catch block to the dispatcher, to that place where controller dispatch method is called.

Dmytro Zavalkin
  • 5,265
  • 1
  • 30
  • 34