Laravel: A brief summary of the lifecycle

Table of Contents

Table of Contents

This article is a compilation of notes on the Laravel Lifecycle, including the Kernel, Middleware, ServiceProvider, and Route, which I think will be of great help to those learning Laravel in terms of understanding the overall look of Laravel and mastering its lifecycle. Laravel framework or investigating the underlying functionality will be of great help.

Laravel Life Cycle

First of all, the entry point of Laravel applications is public/index.php.
And index.php does three main things.

  1. verify that the application is in maintenance status.
  2. run Composer autoload.
  3. register using bootstrap/app.php.

Next, let's take a look at the key points of bootstrap/app.php.


From the code, we can understand that it registers the core interfaces of Exception Handler, Http Kernel, Console Kernel applications.
Exception Handler manages the handling of requests when exceptions occur, and we will talk about this part later. Here we will mainly look at the main character of the life cycle Kernel.


Requests received by the application are passed to either the HTTP Kernel or the Console Kernel depending on the request method.

  • HTTP Kernel: app/Http/Kernel.php. Used to handle HTTP requests.
  • Console Kernel: app/Console/Kernel.php. Used to handle other requests.

In Http Kernel, you can define middleware list. Such as http session, csrf token ... and so on.
It also contains a list of middleware related to Route.

  • $routeMiddleware: list of application route middleware
  • $middlewareGroups: A list of the application's route middleware groups

Let's move on to the important ServiceProvider in the Kernel.


The Kernel will download and apply ServiceProvider on its own after booting.
The ServiceProvider is responsible for the initial startup of the components of the application framework (DB, Routing, Validation, Queue, etc.).
The provider list is defined in config/app.php. You can see what ServiceProviders be defined.
The source files for each ServiceProvider are stored in the App/Providers/ folder.
Among them, the ServiceProvider related to Route is RouteServiceProvider, so let's see what is available here.


The source file is App/Providers/RouteServiceProvider.
The routes/web.php and routes/api/php path settings are automatically loaded into the processing inside. As you can see, the web page path is set by routes/web.php and the API path is set by routes/api/php.


The flow is roughly, public/index.php -> bootstrap/app.php -> ServiceProvider -> routes/web.php, routes/api/php.

Reference Lifecycle