In this section, I will only focus on the Laravel request life cycle. How Laravel process your request. Everyone uses routes, controllers, views, models, etc but many of them don't know how Laravel handles your request. At the end of this section, you will feel more comfortable with Laravel. I am using Laravel 7.
I have used Laravel 5 and 6 versions earlier and there is a quite minor difference. So 5 and 6 versions users can also follow this.
Before starting this I want you all to see this diagram, as it will surely help you to understand the Laravel Request Lifecycle very clearly.
So, as we see the above figure, Laravel first hit
public/index.php. In this file, Laravel will first load all composer dependencies and then create the instance of the application from
bootstrap/app.php. This is the very first step.
Now from here, the request will either go to HTTP kernel or Console kernel. Its totally depend on you:
1. If you are working on the web application then your request will go to HTTP kernel which is located in
2. If you are working on the command line application then your request will go to Console kernel which is located in
So, now I am assuming that you are using the web application. In HTTP kernel i.e. in
App\Http\Kernel.php you will see that Kernel class extends the
Illuminate\Foundation\Http\Kernel class. In this class, you will see an array of bootstrappers that will run before each request.
As you see this image, these bootstrappers will be responsible for detecting the environment variables, loading all configurations, error handlings, logging configurations, and all other settings that is need before the request is actually handled.
HTTP kernel also has a list of middlewares from which each request will pass. These middlewares handle reading and writing the HTTP session, determine if application is in maintenance mode, CSRF token verification, and many more things.
As you see the above image in which I have shown the array of bootstrappers you will see that there is a class i.e. RegisterProviders which is responsible for loading all the configured service providers. All of the service providers for the application are configured in the
config/app.php configuration file's
In each provider class, you will mostly see two common methods i.e.
boot(). One by one
register() method will called in each class. Then once all providers have been registered than
boot() method will be called.
In Laravel Docs, It is clearly mentioned that Service providers are responsible for bootstrapping all of the framework's various components, such as the database, queue, validation, and routing components. Since they bootstrap and configure every feature offered by the framework, service providers are the most important aspect of the entire Laravel bootstrap process.
After all the above procedure request will be handed off to the router.