Request Life Cycle of Laravel

When we start working on a framework or tool or service, we should have a detail knowledge on how it works from the scratch. This knowledge will make us more comfortable with that platform.

This blog is to help you get to know about Laravel’s ‘Request Life Cycle‘ i.e, how this framework processes the given request in different stages and provide the response to the user. We will look into this as step by step process for better understanding.

Auto Loader

As a first step, the request will be triggered from the user’s browser, then it will reach web server. Web server (Apache or Nginx) will redirect the given request to Laravel public/index.php file which is simply a starting point for loading the rest of the framework. It loads the auto loader files which is generated by composer.

Then it retrieves an instance of the Laravel application from bootstrap/app.php script. Laravel itself creates an instance of the application, is the initial/first step.

Kernel

Next step will occur on the Kernel part of the application.

The incoming request is sent to either the HTTP kernel or the console kernel, depending on the type of request that is entering the application .These two kernels serve as the central location that all requests flow through.

HTTP kernel, which is placed in app/Http/Kernel.php. It just receive a Request and return a Response. Bootstrappers that are defined by the Kernel class, which configures error handling, configure logging, detect environments and other tasks to be done before the request handled.

HTTP Kernel will define the list of middleware that are passed through before handled by application.

Service Providers

Next step of the kernel is to load service providers as part of the bootstrapping action. Providers that are needed for the application are placed in config/app.php configuration file.

While the register method calls, all the providers will be registered. Once all providers are registered, then boot method will be called.

Dispatch Request

Once the application have been bootstrapped and all service providers are registered and booted, the request will be handed over to the router for dispatching. The router will dispatch the request to a route or controller, as well as run any route specific middleware.

Router:

Now request will be dispatched by the Router and it will end up with the views as shown below:

Router will direct the HTTP Request to a Controller or return a view or responses directly by omitting the controller. These routes will be placed in app/routes.php.

Controller app/controllers/ performs specific actions and sends data to a View.

View app/views/ formats the data appropriately, providing the HTTP Response.

The above steps are clearly explained in the diagrammatical view below.

 

Laravel - Request Life Cycle

Let’s see an example for a request

Step 1

The user input http://xyz.com in the browser and hits ‘enter’.

Step 2

Once the user hit this URL, the browser will send the page request over the Internet to the web server.

Step 3

Web server will receive the request and analyze the request information. In web server’s configuration file, site’s root path is specified. Based on that, web server will look for index.php file in that root directory, since URL doesn’t contain any sub directory or any other routes.

Step 4

Web server will direct the request to public/index.php file of laravel application.

Step 5

In this step, PHP Interpreter is executing the code contained in the index.php file from the request. During this step, auto loader files which are generated by composer will be loaded.

Step 6

Then it will create the laravel instance and bootstrapping the components of laravel.

Step 7

The kernel will receive the request, load the service providers and will be redirected to router.

Step 8

Router will render the view file content and return back to web server.

Step 9

Web server receives the output from PHP and sends it back over the Internet to a user’s web browser.

Step 10

The user’s web browser receives the response from the server, and renders the web page on user’s computer.

Conclusion

By understanding the flow of the request life cycle, we will have more confidence while developing a application. Also it will provide enough skill to debug the code in a faster way. We can easily track the issues during some unexpected situations.

Gayathri.V,
PHP Senior Developer,
Mallow Technologies.

Leave a Reply

%d bloggers like this: