Explain the operation mechanism of PHP's YII framework and the _php skills of its routing function

Source: Internet
Author: User
Tags autoload yii

Operating mechanism overview
every time a Yii application starts processing an HTTP request, it does an approximate process.

    • The user submits a request to the portal script web/index.php.
    • The portal script loads the configuration array and creates an application instance to process the request.
    • The application resolves the requested route through the request application component.
    • The application creates a controller (Controller) instance to handle the request specifically.
    • The controller creates an action instance and executes the associated Filters (Access filter) for the action.
    • If any one of the filters fails, the action is canceled.
    • If all the filters are passed, the action is executed.
    • The action loads a data model, which is typically loaded from the database.
    • The action renders a view and provides it with the desired data model.
    • The rendered results are returned to the response (response) Application component.
    • The response component sends the rendering results back to the user's browser.

The following diagram shows how the application handles a request.

Boot boot (Bootstrapping)
boot boot is the process of preparing an environment before an application begins parsing and processing a newly accepted request. The boot boot will be done in two places: the entry scripts (Entry script) and the application body (application).

In the entry script, you need to register the class file loader for each class library (class Autoloader, short name automatic loader). This includes mainly the Composer loader that is loaded through its autoload.php file, and the Yii loader loaded through the Yii class. The portal script then loads the applied configuration (configuration) and creates an instance of the application principal.

In the constructor of the application principal, the following boot work is performed:

    • Call Yii\base\application::p reinit () (Pre-initialization) method to configure some high-priority application properties, such as the Yii\base\application::basepath property.
    • Register Yii\base\application::errorhandler.
    • Initializes the properties of the application with the given application configuration.
    • By calling the Yii\base\application::init () (initialization) method, it will invoke Yii\base\application::bootstrap () sequentially to run the boot component.
    • Load extension manifest file (extension manifest file) vendor/yiisoft/extensions.php.
    • Create and run the boot component (bootstrap components) for each extension declaration.
    • Create and run each application component and each module (modules) component (if any) that is declared in the Applied Bootstrap property.

Because the boot work must be done before each request is processed, it is important to make the process as lightweight as possible, and optimize this step as much as possible.

Please try not to register too many boot components. It only needs to be used if he needs to function in the entire lifecycle of the HTTP request processing. Give an example that uses it: A module needs to register additional URL parsing rules, and it should be listed in the bootstrap attribute of the application so that the URL parsing rule will take effect before parsing the request. In other words, for performance's sake, most components should be loaded on demand, not all in the boot process, except for a few operations like URL parsing. )

In a production environment, you can turn on bytecode caching, such as APC, to further minimize the time it takes 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 portal script creates the application instance.


the entry file for Yii
this uses a Third-party configuration management plug-in: Marcovwout, to manage the configuration of Yii, I will not say the details. The rest is just some basic global variable settings. Into the yii::createwebapplication array, and then call the Run method, a Web application is running, yes, the abstraction to the highest level is this: I went to a container inside the corresponding configuration, This application can then be run properly based on the configuration.
Two more important methods in Yiibase (Import,autoload)

This uses a Third-party configuration management plug-in: Marcovwout, to manage the configuration of Yii, I will not say the details. The rest is just some basic global variable settings. Into the yii::createwebapplication array, and then call the Run method, a Web application is running, yes, the abstraction to the highest level is this: I went to a container inside the corresponding configuration, This application can then be run properly based on the configuration.

Routing
when the portal script calls the Yii\web\application::run () method, its first operation is to parse the input request and then instantiate the corresponding controller action to process the request. This process is referred to as a boot route (routing). In Chinese, both verbs and nouns

Parsing routing

The first step in routing is to resolve incoming requests to a route. As we described in the Controller (Controllers) section, a route is an address used to locate a controller action. This process is implemented through the request application component's Yii\web\request::resolve () method, which invokes the URL manager for substantive request parsing.

By default, the incoming request contains a get parameter named R, whose value is considered a route. However, if Yii\web\urlmanager::enableprettyurl is enabled, more processing is done when the requested route is determined. Please refer to the parsing and generating section of the URL for specific details.

If a route is ultimately impossible to determine, the request component throws a Yii\web\notfoundhttpexception exception (the famous 404).

Default route

If the incoming request does not provide a specific route, which is generally the case for the first page request, the default route specified by the yii\web\application::d Efaultroute property is 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 intercept routing)

Sometimes, you will want to temporarily adjust your WEB application to maintenance mode, and all requests will display the same information page. There are, of course, many ways to achieve this. The easiest and quickest way to do this is to set the Yii\web\application::catchall attribute in the application configuration:

return [
  //...
  ] Catchall ' => [' site/offline '],
];

The Catchall property needs to pass in an array of arguments, the first element of the array is routed, and the remaining elements (in the form of a name-value pair) Specify the parameters that are bound to the operation.

When the Catchall property is set, it replaces all routes resolved from the input request. If this is the case above, the same site/offline will be used to handle all incoming requests.

Create an action

Once the request route is determined, the next step is to create an action object to respond to the route.

The route can be separated into pieces by a slash in the inside, a chestnut, site/index can be decomposed into site and index two parts. Each fragment is an ID that points to a module (module), CONTROLLER (Controller), or action (action).

Starting with the first fragment of the route, the application creates the module (if any), the controller, and the operation through the following process:

    • Sets the application principal as the current module.
    • Checks whether the current module's yii\base\module::controllermap contains the current ID. If so, a controller object is created from the configuration in the table and then skips to step five to perform subsequent fragments of the route.
    • Check to see if the ID points to a module in the list of modules in the Yii\base\module::modules property in the current module. If so, a module object is created from the configuration in the module table and then the next route is parsed by step two, with the newly created module as the environment.
    • Treat the ID as the controller ID and create the Controller object. Use the next step to parse the remaining fragments in the route.
    • The controller searches for the current ID in his yii\base\controller::actions (). If found, it creates an action object based on the configuration in the mapping table, whereas the controller attempts to create an inline action (inline action) that corresponds to that ID and is defined by an action method.

In the above steps, if any errors occur, a yii\web\notfoundhttpexception is thrown, indicating that the route-guided process failed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.