Laravel Request Lifecycle

When I first started out as a PHP Developer I had to learn a framework as well because of my job, and that was Laravel.

And one of the most basic concepts I had to learn was the Request Lifecycle. Or so I thought. I had myself going back and forth a couple of times reading the documentation over and over, finding something new every time.

While understanding the request lifecycle we will also have to go through Autoloads, Kernels, Service Providers, Dispatchers and Routers
(The Service Provider will be an article in itself).

The first entry in our application will be index.php.

As soon as we are in the index.php Laravel will include the composer autoloader, which will generate the class loader for our application.

require __DIR__ . '/../vendor/autoload.php';

Then it will bootstrap the framework and generate an instance of the application

$app = require_once __DIR__ . '/../bootstrap/app.php';

After the application is instantiated, the request will be handled by our Kernel which is in fact 2 Kernels! We have an HTTP Kernel and a Console Kernel.

The kernel extends Illuminate\Foundation\Http\Kernel which defines an array of bootstrappers that will be run before the request is executed.

Those bootstrappers configure Error Handling, Logging, and more.

Besides that, the HTTP Kernel will define a list of Middlewares that the request will pass through forward and backwards as it is handled.

$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

And the kernel simply handles the incoming request as follows

$response = $kernel->handle($request = Illuminate\Http\Request::capture());

You can imagine the Kernel as a big black box of some sort ( which is your entire Application actually)… you feed it an HTTP Request and it spits out an HTTP Response.

All of this heavy lifting done by the Autoloaders and bootstrappers was amazing, right? It doesn’t end here.

Service Providers

This is by far the most important concept to learn with Laravel.

If in the previous step we created the application, then in this step we right after this where the service providers are registered and the request is handed to the bootstrapped application.

We can find all our service providers in our config/app.php file and it should look something on the lines of…
( I did not include most providers to save space but it creates a general idea of what it looks like )

'providers' => [
/*
* Laravel Framework Service Providers...
*/
Illuminate\Encryption\EncryptionServiceProvider::class,
Illuminate\Filesystem\FilesystemServiceProvider::class
Illuminate\Translation\TranslationServiceProvider::class,

/*
* Package Service Providers...
*/
Middleout\Providers\ApiResponseMacrosServiceProvider::class,
Middleout\Providers\DatabaseMacrosServiceProvider::class,
Middleout\Providers\CollectionMacrosServiceProvider::class,

/*
* Application Service Providers...
*/
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
],

Laravel will iterate through the list of providers and call the register() method on every each of them.

After it has done this, it will call the boot() method on each of them, so the providers can depend on every container binding that is being registered and available by the time their boot() method, is called.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Coman Cosmin

Coman Cosmin

128 Followers

I convert real world problem into technical solutions. I do research and make robots.