Laravel Request Lifecycle Overview
Tags: laravel, blog
Published: 2023-02-21 16:51:43

Would you like to learn about the Request Life Cycle in Laravel? This Article refers to the series of stages through which the framework processes a given request, ultimately providing a response to the user. Let’s examine this process step-by-step to better understand it.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vxrc7payioail19ma8bo.png)

## Entry Point (Auto Loader)

The starting point for all requests in a Laravel application is the public/index.php file. This file is responsible for directing all incoming requests to the correct location within the application.
When a request is made, the index.php file first loads the auto-loader files created by Composer. This allows for the dynamic loading of classes and dependencies as needed.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tglc2j994zd4i3bvi6oq.png)

After the auto-loader files have been loaded, the **index.php** file retrieves an instance of the Laravel application from the **bootstrap/app.php** script.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k2runtj6xwm7wwb2qpfj.png)

## Kernel
Once the application instance generated ✅
The Incoming request will be handled by the kernel. There are two types of kernel in Laravel
1) HTTP kernel
2) Console kernel.
Incoming Request can be handled by either HTTP kernel or Console kernel depending on the request type.
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 also Register & Execute Middleware’s (Manage HTTP session, Detect maintenance mode, verify CSRF token, etc.)

## 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.

All of the service providers for the application are configured in the config/app.php configuration file’s providers array.

First, the register method will be called on all providers, then, once all providers have been registered, the boot method will be called.

Service providers are responsible for bootstrapping all of the framework’s various components, such as the database, queue, validation, and routing components.

* Bootstraping involves, Initializing configurations, detecting application environment, configure error handling, configure logging and other task needs to execute before request being handled

## 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.

The Router will handle the HTTP Request and either direct it to a Controller or return a view or response directly without a controller. These routes are defined in the app/routes.php file.

The Controller, located at app/controllers/, performs specific actions and sends the data to a View.

The View, located at app/views/, formats the data and sends it back as an HTTP Response.

That’s All