Laravel Mastery Journey: Request Lifecycle in Laravel (Day1)

Dec 16, 2020ยท

3 min read

In this post,I will be explaining how a laravel application work from receiving a request to returning a response to the user.


Firstly when a url(e.g is requested on the browser for example,the web server(Apache or Nginx) will send the request to public/index.php which is the entry point of all laravel application.

// public/index.php
require __DIR__.'/../vendor/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php';

The first thing the index.php file does is to load the composer generate autoloader file vendor/autoload.php and then secondly to create an instance of the application from the bootstrap/app.php so it is ready to handle the request from the web

//Here an instance of the application is created
$app = new Illuminate\Foundation\Application(
    $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
//Here the request goes to the HttpKernel or the ConsoleKernel

Notice that from the bootstrap/app.php file above after the first part where an instance of the application was generate it send the request to either the console kernel or the http kernel. In this tutorial I will focus on the HttpKernel because the request is made using http.

Looking at the app/Http/Kernel.php .The Http kernel extends the Illuminate\Foundation\Http\Kernel. This class has an array of bootstrapers that will be run before the request is executed. They detect environment and load configuration, configure logs and error handling. They also register providers with facades, and then boot providers: The http kernel also has a list of HTTP middlewares (global and for specific routes). Every request passes through all the global middlewares before being handled by the kernel.

The process of handling a request is very simple. Kernel has handle() method, that recieves a Request object and returns a Response object:

Did you find this article valuable?

Support Sylvester Amaechi by becoming a sponsor. Any amount is appreciated!