7. MVC fundamentals

The framework implements the model-view-controller (MVC) design pattern...

The concept of front-controller is managed by the Dispatcher, which is the director of the processing of a Request, dispatching it to the appropriate controller.

The following diagram shows the the default workflow of an application when it is handling a user request:

Kernel::dispatch()

The Kernel choose the application that must handle the Request, based on Kernel configuration, and creates the 2 http objects Request and Response. Than it creates the application Context, and ask it to get the Dispatcher, eventually invoking Dispatcher->dispatch().

Dispatcher->dispatch(Request $Req, Response $Res)

The Dispatcher triggers the DISPATCHER:ROUTE event before it execute routing decisions, so that you can intercept and alter routing decisions. Than, it execute routing decisions based on Request, choosing the appropriate Controller that has to handle the user request. Before asking the Controller to handle the Request, the Dispatcher triggers the DISPATCHER:CONTROLLER event.

Controller->handle(Request $Req, Response $Res)

The Controller handle the Request executing his action. More on controllers action in chapter X.X. Before it finish its action, the Controller must prepare an output to be processed by Response. It can happen in 3 ways: 1) the Controller directly produce an output, using echo; 2) the Controller set an output content to Response: $Response->setContent($output); 3) the Controller ask to render a View: $Response->setView($view);

View->render(Request $Req, Response $Res, $resource)

If a View has been invoked by the Controller, the Dispatcher triggers the DISPATCHER:VIEW event. Than invoke View rendering. The View must produce the output.

The Dispatcher triggers the DISPATCHER:RESPONSE event, allowing you to customize the output just before it goes to the user.

Response->send()

The Disptacher call the Response send() method, which actually flush the output buffer, sending content to the user.

This is the short summary:

  • Kernel::dispatch()
  • event DISPATCHER:ROUTE
  • event DISPATCHER:CONTROLLER
  • Controller->handle()
  • event DISPATCHER:VIEW
  • View->render()
  • event DISPATCHER:RESPONSE
  • Response->send()