The life cycle of a Laravel request


brief introduction

When we use any tool in the real world, if we understand the working principle of the tool, we will be able to use it easily, and the same is true for application development. When you understand how development tools work, you will be more comfortable with them.

The goal of this document is to explain the working principle of the Laravel framework to you from a higher level. Through a more comprehensive understanding of the framework, everything is no longer mysterious, and you will be more confident in building applications. If you can't understand all these terms immediately, don't lose confidence! Try to master some basic things first, and your knowledge level will continue to improve with the exploration of documents.

Lifecycle Overview

First thing

All request entries of Laravel application are public/index.php File. All requests will be directed to this file by the web server (Apache/Nginx). index.php The file does not contain much code, but this is the starting point for loading other parts of the framework.

index.php The file loads the auto load settings generated by Composer, and then bootstrap/app.php The script obtains the Laravel application instance. The first action of Laravel is to create Service Container example.

HTTP/Console kernel

Next, the request is sent to the HTTP kernel or Console kernel (used to process Web requests and Artisan commands respectively), depending on the type of request entering the application. These two cores are the central processing units through which all requests pass. Now, let's focus on app/Http/Kernel.php The HTTP kernel of.

HTTP kernel inherited from Illuminate\Foundation\Http\Kernel Class, which defines a bootstrappers Array. The classes in this array run before the request is executed bootstrappers Configured error handling, logging Test application environment And other tasks that need to be executed before the request is processed.

The HTTP kernel also defines a series of HTTP requests before processing middleware , these middleware processes HTTP session Read/write, judge whether the application is in maintenance mode, and verify CSRF token wait.

HTTP kernel handle The method signature is quite simple: get a Request , return a Response , you can think of the kernel as a big black box representing the entire application, input HTTP requests, and return HTTP responses.

Service Provider

One of the most important actions during kernel startup is to load applications Service Provider All service providers of the application are configured in config/app.php Profile's providers Array. First, all providers' register Method is called, and then after all providers are registered, boot Method is called.

The service provider is responsible for starting all kinds of components of the framework, such as databases, queues, validators, and routing components. Because they start and configure all the features provided by the framework, the service provider is the most important part of the entire Laravel startup process.

Distribution request

Once the application is started and all service providers are registered, Request It will be handed over to the router for distribution. The router will distribute the request to the route or controller, and run all the middleware specified by the route at the same time.

Focus on service providers

The service provider is the most critical part in starting the Larravel application. After the application instance is created, the service provider is registered and the request is handed over to the launched application for processing. The whole process is so simple!

It is very valuable to have a firm grasp of how to build and start a Larravel application through a service provider. Of course, the default service provider of the application is stored in app/Providers Directory.

By default, AppServiceProvider It is empty. This is the best place to add custom startup and service container binding. Of course, for large applications, you may want to create multiple service providers, each with a more fine-grained startup.

Note: For more details, please refer to Analysis of Laravel 5. x Startup Process

give the thumbs-up Cancel Like Collection Cancel Collection

<<Previous: Using Laragon to build the Laravel development environment in Windows

>>Next: Service Container