4

When User A decides he want to pay a visit to http://example.com/ it all begins with a Request. A short moment later he is given a Response. In this cycle Request-Response a lot is going on, in different frameworks it's decoupled in different components among them Routing, Dispatching, etc.

I'm looking to make a simple experimental MVP Framework for my learning, trying to use as many best practices, heck who knows it might even be worth using when I'm done.

I'm not sure how I should decouple this cycle in to these different components and I'm not even sure of what each component is responsible for (Thinking SOLID, DRY, SRP, etc). That is what I am asking, looking at different frameworks these components seem to have different use-areas from time to time.

What is each of these components responsible for and in what order are the used?

Request, Response, Dispatcher, Router, Front Controller, Bootstrap, Events.

1 Answer 1

1

Bootstrap

The bootstrap file is the entry point of your application (index.php). It contains code in the global scope (and this is the only place, where that should be the case). In an outline, it does

  • create a request object from the request
  • create a response object according to the requested resource type
  • ask the router for the responsible controller for the current request
  • create the controller and call its main method, providing it with the request and respond objects (this often is called dispatching)
  • echo the response

Request

The request object is used to abstract from the web environment. In the end, it is just a data container (often a copy of $_REQUEST) that allows you make subsequent calls to other controllers (see HMVC).

Response

The response object is managing the output. You may have different response classes for XHTML, HTML5, PDF, JSON, ..., you_name_it with an identical interface. It is responsible for messaging warnings and errors to the user, too.

Router

The router knows, which controller is assigned to which kind of URL. Given the request, it makes a decision about the controller and returns that to the bootstrap.

Dispatcher

A dispatcher is used in different situations. You may think of it like a map and call thingest. For our case, the bootstrap file and the router together make up a dispatcher. A dispatcher is also used to map events to the various event handlers (plugins) and execute them (often using the Observer pattern).

Front Controller

A dispatcher mapping URLs to controllers and executing them dependend on the request is also called a front controller.

Events

the event mechanism is used to decouple systems. You can for example create plugins that take influence on the result of the router. The only thing you have to do for that is to dispatch some events (eg. onBeforeRoute or onAfterRoute) in the routing method. onBeforeRoute would get the request and could change it, before the router even looks at it; onAfterRoute would be able to change the router's output. This is just a small example of whjat you can do with events.

2
  • What does the Router return to the so far non-initialized Controller? I'm not sure I follow the difference between the dispatcher/front controller objects. Commented May 21, 2013 at 16:13
  • Sorry, ment bootstrap instead of controller - going to correct that. -- Thats the point: Bootstrap+Router == Front Controller (which in fact is a special Dispatcher)
    – nibra
    Commented May 21, 2013 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.