Learning a new framework is bit more challenging if you do not know how it works. It is important to learn about request/response lifecycle of the framework.
What do I mean by request/response lifecycle? I mean to say when user makes a request on a browser how does laravel app process this request and respond back.
Once you learn about how laravel process earch request and what stages it goes through while processing this request, you become more comfortable working with the framework.
In this tutorial I will walk you through request/response cycle of laravel framework.
Entry point of Laravel application?
Let say that you are a user who goes to your chrome browser and hit a website. Let's assume that website you are looking for is served using laravel framework.
Let's look at following diagram to understand better:
At this point you are opening your browser and hitting some website which serve laravel application. Your request first goes from your browser to remote web server which could be running nginx or apache server.
Let us also assume that on this remote web server there is a web directory called /var/www/laravel-app. This is where laravel application code is hosted.
Now, nginx or apache web server is pointing to index.php file located in /var/www/laravel-app/public directory. This public/index.php file will be your entrypoint of laravel application.
What is public/index.php file in laravel?
Now, so far we know that public/index.php file is the entry point of laravel application let's look at the content of this file and understand how request is going through different stages.
First line we see requires vendor/autoload.php file you might ask what is this file and why do we need this file?
Well, we know that laravel framework comes with composer.json file which we use to install third-party libraries into our laravel application right if you did not know there you go you learned something new.
This vendor/autoload.php file will load all third party libraries mentioned in composer.json file. Once this dependencies are loaded in the application we can use these libraries in our app.
Next, laravel requires bootstrap/app.php file this file creates singleton classes needed in order to run laravel application. Laravel then creates instance of the application / service container.
Once application instance is created laravel request is being handled by Kernel class as you can see below:
$kernel = $app->make(Kernel::class); $response = $kernel->handle( $request = Request::capture() )->send(); $kernel->terminate($request, $response);
So far, we know in general how laravel creates application instance and request is handled by laravel Kernel class and later it is terminated once response is rendered.
What is Laravel Kernel Class?
In order to understand Kernel better we first need to understand type of request we are making. There are two types of request in Laravel:
- browser request
- cli or command line request
Depending on type of the request laravel loads Kernel Class accordingly. There are two types of Kernel in Laravel as seen in below diagram:
In our case we are making request from the browser. Kernel classes are located in following directories:
- Http Kernel -> app/Http/Kernel.php
- Console Kernel -> app/Console/Kernel.php
Assuming you are a intermediate developer we are not going to deep dive in each steps however we will learn from the surface first.
What does Http Kernel class do in Laravel ?
When request enters in Http Kernel class it goes through list of boostrapers defined in Illuminate\Foundation\Http\Kernel class.
The purpose of these bootstrappers is to:
- configure logging
- configure error handling
- detect the application environment
- perform other tasks needed before the request is handled
One of the important steps in bootstrapping process is to load the service providers. Service providers are responsible for bootstrapping various components of laravel application like:
- database
- queue
- validation
- routing etc...
After bootstrapping process request passes through middlewares defined in Kernel class. These middleware handle reading and writing of http sessions, verifying csrf tokens and does many more things.
Think Kernel as a big block which recieves the HTTP request and returns HTTP response.
How does laravel loads routing?
During boostrapping process laravel loads App\Providers\RouteServiceProvider purpose of this service provider is to load all routes defined in routes directory of your laravel application.
Laravel routes are mapped to controller methods. Each route points to specific controller method where it handles the request and returns the reponse.
Laravel request passes through different route middlewares before request reach to controller method. Purpose of route middleware is to run some logic that needs to happend before request is sent to controller.
Laravel9 route identifies the incoming url and passes this request to controller via different route middlewares.
Laravel controller then handles the request loads the appropriate view and respond back with http response object.
Laravel then parses the response object and sends contents to user's browser.
Important things to learn from bootstrapping process
Now, that you know service providers are important and key to bootstrapping process focus on them, You can write custom packages using service providers to create your own laravel modules.
We will learn about service providers, service container etc in details in upcoming tutorials.