We all knowLaravelThe entire execution process from request to response can be divided into four stages: program startup preparation stage, request instantiation stage, request processing stage, response sending and program termination stage; So, what is included in each stage? Let's take a brief look at the entire life cycle of the laravel framework from request to response.
Program startup preparation phase
Service container instantiation
The instantiation and basic registration of the service container includes the registration of the service container itself, the registration of the basic service provider, the registration of the core category name and the registration of the basic path of the application. The registered service is only a specific class name, and the object is instantiated through the reflection mechanism, and the dependencies in the constructor are automatically resolved through the reflection mechanism.
Core class instantiation
Core class instantiation is an automatic instantiation object of the service container obtained by registering the service with the core class name after the service container is instantiated. For example: Kernel class instantiation in index.php:
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
Request instantiation phase
The request is a request report sent by the client Document, including request line, request header and request entity. Laravel classifies and saves it in the instance object of the Illuminate\Http\Request
class, which means converting the request into an instance object. The creation of the request instance is completed through the capture() static method of the Illuminate\Http\Request
class, that is:
$request = Illuminate\Http\Request::capture();
But in the capture() method, you can see Laravel's request Instances are created based on Symfony request instances. Symfony instantiates requests through PHP's global arrays $_GET, $_POST, $_COOKIE, $_FILE and $_SERVER as parameters.
Request processing phase
The request processing phase first prepares the environment for request processing, including environment loading, service provider registration, etc., and then The process of processing request instances through middleware and distribution control through routing and controllers, so that different requests are processed through corresponding handlers and responses are generated.
Response sending and program termination phase
Response sending
Laravel’s response processing class is Illuminate\Http\Response
class, the bottom layer of this class is also Symfony's Response class. The sending of the response includes two parts: the sending of the response header information and the sending of the response body content.
Program termination
In Laravel, the termination program mainly completes the call of the termination middleware.
The above is the entire content of this article. For more laravel content, please pay attention to the laravel framework introduction tutorial.
Recommended related articles:
Diagram overview of PHP life cycle, PHP life cycle_PHP tutorial
##[ Laravel 5.2 Documentation] Architecture--The life cycle of a request
The above is the detailed content of What are the stages of the Laravel framework life cycle? Introduction to the four stages of Laravel life cycle. For more information, please follow other related articles on the PHP Chinese website!