Laravel 4 life cycle: Request, Route and Router object

laravel 4 route router and request

Today, I’ll share you about one of my experiment when working with Laravel 4 application: distinguish Request, Route, and Router object of the application instance. All these object play a part in the routing phase, the step that decide which method of which controller will be call on each time the Laravel application run.

The Request object

A Request object is mapped from the raw HTTP request that received from web server. There is no information about “controller” or “method” stored in it. This object is created in two ways in Laravel:

  • Created directly from PHP superglobal when the application is booted by web server. This is most common situation when a user reach your application from an URL.
  • Created manually when runnin a unit test

In this article, I’ll talk about the Request object that created by first method. This object contains information about:

  • All parameters from $_GET
  • All parameters from $_POST
  • All cookie information from $_COOKIE
  • All uploaded file information from $_FILES
  • All server information from $_SERVER

The Request object is the first object that created in the application life cycle among these three. It is created inside the constructor of the Application  class.

 The Route object

A route is a 3-tuple of these information:

  • Which HTTP method is currently handled (GET, POST, PUT or DELETE, …)
  • Which URI is currently served (/foo, /bar/1, or /foo/1/bar, …)
  • Which action will be executed. An action is whether a closure object or a method of some controller, both will return a response in the end (an HTML page or a JSON content, …)

A route may contains a name defined in the routes.php  file (and any files that included by that file). Without this object, Laravel cannot know how to continue serving the current request because it cannot decide which is the correct action to do (the C part in MVC definition). The Route object is the last created object among these three

The Router object

As a result, a question is asked: “How is the route object created?”. Well, the answer is in this section: the Router object. It is created independently with the Request object via the RoutingServiceProvider which is called after the application is created (and after the Request object is created). This object keep the reference to the current Request object and the matched Route object that matched with defined rules.

When created, the Router object:

  • creates a collection of registered routes
  • allows new route registered inside routes.php
  • look up it registered route collection to choose the correct route with the current request

So, we can think this object as a link between the HTTP request and the Laravel controller.

Conclusion

Each of three objects keep some information that may be useful in different situation. In my experience:

  • When you want to known information about the HTTP request (server, cookie, get and post params, … ), use the Request object
  • When you want to known information about the current action (controller, method, closures, …), use the Route object
  • If you does not decide what information will be get, use the Router object which can access both of above. One of my package can be used as an example for this situation.

Happy coding!

 

Leave a Reply

Your email address will not be published. Required fields are marked *