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.
First of all, the entry point of Laravel applications is
And index.php does three main things.
Next, let's take a look at the key points of
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.
app/Http/Kernel.php. Used to handle HTTP requests.
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.
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
Among them, the ServiceProvider related to Route is RouteServiceProvider, so let's see what is available here.
The source file is
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
The flow is roughly,
bootstrap/app.php -> ServiceProvider ->