As you know, Laravel is a framework with rich features. but you ever wondered How Laravel handles these features? How request handles at different stages when you hit URL on the browser and return response? If these questions arise in your mind then you are on the right track. In this post, we'll discuss the backbone of Laravel called "Request Lifecycle".
Request Lifecycle has different terminologies like Autoloader, kernel, Service Providers, Dispatch Request, and Router. Once you understand all of the terminologies in detail you will be more confident and comfortable with the framework and can extend different functionalities however you like.
So let's dive into the sea...
When a request is made to laravel it will first call public/index.php
file. its starting point of each request. Index.php file just contains a few lines of code which will perform initialize actions.
So just open public/index.php
file and look into it.
require __DIR__.'/../bootstrap/autoload.php';
It will first include composer autoload (Which will automatically generate class loader for our application) so that we don't have to worry about manual loading any of our classes later on.
$app = require_once __DIR__.'/../bootstrap/app.php';
After that, it will bootstrap laravel framework to use and generate an application instance.
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);
$response->send();
$kernel->terminate($request, $response);
Once the application instance generated, the Incoming request will be handled by the kernel. There are two types of kernel in laravel HTTP kernel & Console kernel. So Incoming Request can be handled by either HTTP kernel or Console kernel depending on the request type. These two kernels are the central point of each request.
You can see, the Handle
method of the kernel is simple. It takes a request
, processes it, and returns a response
to the user.
Let's understand HTTP kernel which is placed in app/Http/Kernel.php
. If you open this file its extends Illuminate\Foundation\Http\Kernel
which is base class of this kernel. Its main duty is to bootstrap framework functionality like Configure Error Handling, Loads Configuration Files, Configure Logging, Detect Environments, and other tasks to be done before the request handled.
HTTP kernel also Register & Execute Middlewares (Manage HTTP session, Detect maintenance mode, verify CSRF token, etc.) which will be called on each request based on configurations.
With the configuration of middleware and other functionalities, HTTP kernel also loads Service Providers. Its most important component of framework and its responsible for bootstrapping framework core features like Database, Routing, Queueing, etc. In short, every feature provided by laravel framework will be bootstrap by Service Providers. Service Providers register all class bindings in the register
method and then call the boot
method. Providers that are needed for the application are placed in config/app.php
configuration file. You can create your own Providers inside app/providers
and define it into config/app.php
which will be also executed during the bootstrap process.
Thus Bootsraping involves, Initializing configurations, detecting application environment, configure error handling, configure logging and other task needs to execute before request being handled. Apart from this it also registers middleware like Verifying CSRF Token and Reding & Writing HTTP Sessions etc. and bootstraps service providers which are the heart of the framework.
Once the bootstrapping process completed, the request is dispatched to Router. Routers are placed in app/routes.php
.These Router calls controller methods as well as run middlewares or returns a view as a response.
So that's it, we have gone through the lifecycle of laravel, understand associated terminologies, and hope it's clear now. The center of attraction here is Bootsraping Process and Service Providers which are the most important part.
Thank you for reading.
Top comments (2)
Instead of:
It should be:
Informative and to the point. Thanks.