Laravel Request Lifecycle
Tags: laravel, architecture
Published: 2020-07-15 14:10:29

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 <code>public/index.php</code> 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 <code>public/index.php</code> file and look into it. 
```php
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.
```php
$app = require_once __DIR__.'/../bootstrap/app.php';
```
After that, it will bootstrap laravel framework to use and generate an application instance.
```php
$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 <code>Handle</code> method of the kernel is simple. It takes a <code>request</code>, processes it, and returns a <code>response</code> to the user.

Let's understand HTTP kernel which is placed in <code>app/Http/Kernel.php</code>. If you open this file its extends <code>Illuminate\Foundation\Http\Kernel</code> 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 <code>register</code> method and then call the <code>boot</code> method. Providers that are needed for the application are placed in <code>config/app.php</code> configuration file. You can create your own Providers inside <code>app/providers</code> and define it into <code>config/app.php</code> 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 <code>app/routes.php</code>.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.