Home > PHP Framework > YII > body text

How to define routing in yii framework

(*-*)浩
Release: 2019-12-11 11:47:48
Original
3907 people have browsed it

How to define routing in yii framework

Routing and URL generation

When a YII application starts processing a request, what it does first It is to convert the requested URL into a route. The role of routing is to subsequently instantiate corresponding controllers and operations in order to process requests. The entire processing process is called routing.                                                                                                               (Recommended learning: yii framework)

The reverse process of routing is called URL generation, which refers to using the given routing and parameter information to generate a URL. When the generated URL is used to make a request, the routing processing process can parse it again and restore the original routing and parameter information.

The URL manager is mainly responsible for routing and URL generation, which is registered as an application component. The URL manager provides the method parseRequest() to parse the request and parse out the routing and parameter information.

The method createUrl() is used to generate a URL from the given route and parameter information. By configuring the URL manager in the application configuration, you can enable your application to recognize any URL format without modifying existing program code. For example, you can use the following code to generate a URL.

use yii\helpers\Url;
// Url::to() calls UrlManager::createUrl() to create a URL
$url = Url::to(['post/view', 'id' => 100]);
Copy after login

Depending on the configuration of the URL manager, the URL generated by the above code looks like the following. If this URL is subsequently requested, it will be parsed into the original route and parameter information above.

/index.php?r=post/view&id=100
/index.php/post/100
/posts/100
Copy after login

URL format

URL manager supports two URL formats: default URL format and pretty URL format. The default URL format uses a query parameter r to pass the route, and other parameters are placed in the URL in the normal way.

For example, the URL /index.php?r=post/view&id=100 has a route of post/view and a parameter id of 100. The default URL format does not require any configuration of the URL manager.

The beautiful URL format uses an additional path after the entry script name to display the route and related parameters.

For example, the additional path of URL /index.php/post/100 is /post/100, the route displayed is post/view and the parameter id is 100.

If you want to use this URL format, you need to design a URL rule set based on actual needs. You can switch between these two URL formats by modifying the value of the attribute enablePrettyUrl in the URL manager.

Routing

The work of routing can be divided into two steps:

1. Parse a route and Relevant parameters;

2. The controller operation that generates the response based on the route is used to process the request.

When using the default URL format, parsing out the route is very simple, just get the value of the parameter r;

When using the beautiful URL format, the URL manager will detect the URL rules set to find the route that matches the request. If no matching rule is found, an exception yii\web\NotFoundHttpException will be thrown.

Once the route has been parsed from the request, the next thing to do is to create the controller action associated with that route. Routes are slash-cut into several parts, for example, site/index will be cut into site and index.

Each part is an ID, they may point to a module, controller or operation. Starting from the first part of the route, the following steps should be performed to create the module (if any), controller and action:

1. Set the application body to the current module.

2. Check whether the current module's yii\base\Module::controllerMap contains the current ID. If so, a controller object will be created based on the configuration in the table, and then jump to step five to execute subsequent fragments of the route.

3. Check whether the ID points to a module in the module list in the yii\base\Module::modules attribute of the current module. If so, a module object will be created based on the configuration in the module table, and then the newly created module will be used as the environment to jump back to step two to parse the next route.

4. Treat this ID as a controller ID and create a controller object. Use the next step to parse the remaining fragments in the route.

5. The controller will search for the current ID in its yii\base\Controller::actions(). If it is found, it will create an action object based on the configuration in the mapping table; otherwise, the controller will try to create an inline action corresponding to the ID and defined by an action method.

If any errors occur in the above steps, the application will throw an exception yii\web\NotFoundHttpException, which means that routing processing failed.

The above is the detailed content of How to define routing in yii framework. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
yii
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template