In this post we will have a look at Laravel Request Lifecycle. When we use frameworks or programming technologies understanding the flow how it works allows us to use it more efficiently and confidently.
We will have a step by step flow of the laravel request. so lets begin-
Every request to a Laravel application starts with index.php file. The web server directs all the requests to index.php. The index.php is a strting point to load the entire framework.
The index.php loads the autoloader definition generated by the composer and then gets the laravel application instance from bootstrap/app.php file. For laravel application to run the very first step is to create an instance of laravel application.
In the next step the request is sent to the kernel. Depending on the type of request it is sent to either HTTP kernel or the console kernel. These two kernels are central location through which all the request flow. HTTP kernel is placed in app/Http/Kernel.php.
Before executing the request the HTTP kernel defines an array of bootstrappers that execute before the request is executed. These bootstrappers configure logging, detecting application environment, error handling etc. before the actual request is handled.
The HTTP kernel also defines the HTTP middleware through which all the requests must pass before handled by the application. The middleware checks if the application is in maintenance mode. It handles the session Reading writing and verifies csrf token. The middleware is like a security gate the request has to pass through.
The HTTP kernel simply handles Getting requests and returning response. The HTTP kernel represents the entire laravel application.
In the next step the kernel loads the service providers required for laravel application. To configure service providers we need to define them in the config/app.php file’s providers array. First the register method is called on all providers once all service providers have been registered the boot method will be called.
Service providers are one of the most important parts of the laravel framework. Service providers are useful to bootstrap laravel framework components like queue, database, validation and routing components.
After registering all the service providers the request is sent to the router for further proceeding. The router will dispatch the request to a route or a controller function and also run the middleware if configured.
The router will direct the HTTP request to a controller function or return a view. The routs are placed in app/routes.php. These routes are actually end points that point to a controller function.
When the request is passed to a controller function the function includes code related to business logic and returns a view as a response to the user.
This is the flow of laravel Request Lifecycle. Each request in the laravel application goes through above mentioned steps.