Home >Backend Development >PHP Tutorial >Detailed explanation of the operating mechanism of PHP's Yii framework and its routing function, yii operating mechanism_PHP tutorial
Overview of the operating mechanism
Every time the Yii application starts processing an HTTP request, it will go through an approximate process.
The diagram below shows how the application handles a request.
Bootstrapping
Boot bootstrap refers to a process of preparing the environment in advance before the application starts parsing and processing new accepted requests. Startup guidance will be carried out in two places: entry script (Entry Script) and application body (application).
In the entry script, you need to register the class file autoloader (Class Autoloader, referred to as autoloader) of each class library. This mainly includes the Composer autoloader, which is loaded via its autoload.php file, and the Yii autoloader, which is loaded via the Yii class. The entry script then loads the application's configuration and creates an instance of the application principal.
In the constructor of the application body, the following guidance work will be performed:
Because bootstrapping must be done before each request is processed, it is extremely important to make this process as lightweight as possible and optimize this step as much as possible.
Please try not to register too many boot components. You only need to use it if it needs to work throughout the entire life cycle of HTTP request processing. To give an example of its use: if a module needs to register additional URL parsing rules, it should be listed in the bootstrap attribute of the application, so that the URL parsing rules can take effect before parsing the request. (Annotation: In other words, for performance needs, except for a few operations such as URL parsing, most components should be loaded on demand rather than all during the boot process.)
In a production environment, bytecode caching, such as APC, can be turned on to further minimize the time required to load and parse PHP files.
Some large applications contain very complex application configurations that are split into many smaller configuration files. At this point, you can consider caching the entire configuration array and loading it directly from the cache before the entry script creates the application instance.
Yii entry file
A third-party configuration management plug-in is used here: marcovwout to manage Yii configuration. I won’t go into the details. All that's left is some basic global variable settings. Pass the configuration array into Yii::createWebApplication, and then call the run method. Is a web application just running? Yes, abstraction to the highest level is like this: I pass the corresponding configuration into a container, and then The application can run normally based on this configuration.
Let’s talk about two important methods in YiiBase (import, autoload)
A third-party configuration management plug-in is used here: marcovwout to manage Yii configuration. I won’t go into the details. All that's left is some basic global variable settings. Pass the configuration array into Yii::createWebApplication, and then call the run method. Is a web application just running? Yes, abstraction to the highest level is like this: I pass the corresponding configuration into a container, and then The application can run normally based on this configuration.
Routing
When the entry script calls the yiiwebApplication::run() method, the first operation it performs is to parse the input request, and then instantiate the corresponding controller operation to process the request. This process is called routing. (Translation note: It is both a verb and a noun in Chinese)
Resolve routing
The first step in routing guidance is to parse the incoming request into a route. As we described in the Controllers chapter, a route is an address used to locate controller actions. This process is implemented through the yiiwebRequest::resolve() method of the request application component, which calls the URL manager to perform the actual request resolution.
By default, incoming requests include a GET parameter named r, and its value is treated as the route. But if you enable yiiwebUrlManager::enablePrettyUrl, more processing occurs when determining the route of the request. Please refer to the URL parsing and generation chapter for specific details.
If a route cannot be determined in the end, the request component will throw a yiiwebNotFoundHttpException exception (Annotation: the famous 404).
Default route
If the incoming request does not provide a specific route, (usually this is mostly a request for the home page) the default route specified by the yiiwebApplication::defaultRoute attribute will be enabled. The default value for this property is site/index, which points to the index action of the site controller. You can adjust the value of this property in the application configuration like this:
return [ // ... 'defaultRoute' => 'main/index', ];
catchAll routing (full interception routing)
Sometimes, you will want to temporarily put your web application into maintenance mode, so that the same information page will be displayed on all requests. Of course, there are many ways to achieve this. The simplest and fastest way is to set the yiiwebApplication::catchAll attribute in the application configuration:
return [ // ... 'catchAll' => ['site/offline'], ];
The catchAll attribute needs to pass in an array as a parameter. The first element of the array is the route, and the remaining elements will specify the parameters bound to the operation (in the form of name-value pairs).
When the catchAll attribute is set, it replaces all routes parsed from the incoming request. With this setup, the action used to handle all incoming requests will be the same site/offline.
Create operation
Once the request route is determined, the next step is to create an "action" object to respond to the route.
Routes can be split into multiple component fragments using the slashes inside. For example, site/index can be decomposed into two parts: site and index. Each fragment is an ID pointing to a module, controller, or action.
Starting from the first fragment of the route, the application will go through the following process to create modules (if any), controllers, and operations:
In the above steps, if any error occurs, yiiwebNotFoundHttpException will be thrown, indicating that the routing boot process has failed.