Home > Article > PHP Framework > Laravel request to response life cycle
The entire execution process from Laravel request to response can be summarized into four stages, namely, program startup preparation stage, request instantiation stage, request processing stage, response sending and program termination stage.
Program startup preparation phase
Service container instantiation
Instantization and basic registration of the service container, including services The container itself is registered, the basic service provider is registered, the core category name is registered, and the basic path of the application is registered. 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 message sent by the client, including the 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, it can be seen that Laravel's request instance is in the Symfony request instance Created on the basis of. 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 passes the request instance through the middleware Processing and distribution control through routing and controllers, the process of processing different requests through corresponding handlers and generating responses.
Response sending and program termination phase
Response sending
Laravel’s response processing class is the Illuminate\Http\Response class , the bottom layer of this class is also in 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 detailed content of Laravel request to response life cycle. For more information, please follow other related articles on the PHP Chinese website!