When you use a tool, if you understand the internal principles and structure of the tool, then when using this tool, you will be more confident that the tool will be more handy.
Read the official Life cycle documentation for Laravel today.
This article can be seen as a translation of official documents, but it also adds to my current understanding of the Laravel request life cycle, as well as deepening my impression of Laravel's overall architecture.
First things#
First, from the request, all requests from the Web server (Apache/nginx) will be forwarded to public/index.php this file for processing.
index.phpThe code is not much, but this file is a starting point, starting with this file, the rest of the Laravel framework will start to load.
At the same time, index.php bootstrap/app.php An example is obtained, and this is the first thing that the Laravel framework does after getting the request.
Http/console kernels#
With this previously obtained instance, Laravel can generate the kernel that processes the request.
The Laravel itself provides 2 cores, the HTTP kernel and the Console kernel, and 2 cores handle different types of requests, respectively. I understand that the HTTP kernel may be used to handle HTTP requests, while the console kernel is used to handle requests sent from the console. In the article, let's focus on the HTTP kernel for the time being, which is probably the most common.
The HTTP kernel file is located app/Http/Kernel.php , inheriting from Illuminate\Foundation\Http\Kernel the Class Kernel.
In Illuminate\Foundation\Http\Kernel Kernel Class, you define a composed bootstrappers array, and there is a function that iterates through each of the arrays bootstrapper and executes each one bootstrapper . These bootstrappers include error handling, log memory, before the request is processed by the real business logic Configuration tasks such as application environments.
The HTTP kernel also defines a list of middleware (a list of HTTP middleware) that must be processed by these middleware before all requests are processed. These middleware can handle the HTTP session read and write, can determine whether the server is currently in maintenance mode, verify CSRF token (in order to protect the server from CSRF attack) and so on.
Finally, the method that the HTTP kernel eventually uses to process the request handle looks simple. Simply accept one Request and return one Response .
Above these concepts some place perhaps I still can not understand in detail now, want full understanding, need to see Laravel source. But at this stage can first do not need to see the source code.
Just know that the kernel is a black box that can provide you with all the functionality of the server, and you only need to pass the kernel HTTP request, and the black box will return the HTTP response.
Service providers#
In the Kernel Class bootstrappers array, the most important bootstrapper task is to load service provider , and this task is done by the Registerproviders and Bootproviders 2 bootstrapper .
All service provider are configured in config/app.php providers . These ' providers ' start-up methods are: First all provider will execute the Register method, once all the provider are executed The method is called when the Register method is completed boot .
Service Provider is important in that it launches all components of the framework, including databases, queues, data validation, routing components, and so on. It is also because Service Provider starts and configures all the features and functions of the Laravel framework, so that it becomes the final part of the entire Laravel startup process.
Dispatch request#
Once all is done bootstrapper and all Service Provider are registered, the request is finally reached and the request is router router distributed to its next level (usually a controller or another router), of course If any router-related middleware is set, the middleware will be executed first.
Focus on Service providers#
Official documents say, we must focus on the Service Providers ah. After all, the whole Laravel workflow is simply to create an instance, register all the Service Provider, and the request can begin to be processed.
The default Service Providers for the system is placed in the app/Providers directory. By default, AppServiceProvider it is an empty file, and if you want to place a custom initialization task or bind a Service container, this is the right place to do it.
But it might be better to create more of your own service Provider for a large project, with more granular initialization tasks in each Service Provider.
Want to know more #
Laravel Official document: Request Lifecycle
Laravel Study notes Bootstrap source code Analysis
The life cycle of Laravel