Home  >  Article  >  php教程  >  Yii Framework Analysis (8) - URL Management Component

Yii Framework Analysis (8) - URL Management Component

黄舟
黄舟Original
2016-12-27 11:25:301267browse

URL management component mainly provides 2 functions:
1. According to the URL entered by the user, parse out the route for processing this request - which Action of which Controller handles it, and at the same time add the parameter part in the URL to in the $_GET parameter. In every web framework, such a component is needed to perform route distribution work.
2. Create a url based on the route and parameter array. The URL can be hard-coded in the view layer, that is, the URL address is written directly, but this often lacks flexibility and brings costs to later maintenance.

array(
    'components'=>array(
        'urlFormat'=>'path',
        'rules'=>array(
            &#39;/art/<cate:\w+>/<key:\d+>/<id:\d+>/<p:\d+>&#39;=>&#39;article/<cate>/<key>&#39;,
            &#39;post/<id:\d+>/<title:.*?>&#39;=>&#39;post/view&#39;,
            &#39;<controller:\w+>/&#39;=>&#39;<controller>/&#39;,
         ),
    ),
);</action></controller></action:\w+></controller:\w+></title:.*?></id:\d+></key></cate></p:\d+></id:\d+></key:\d+></cate:\w+>

The above is the configuration of a url management component, with a total of 3 rules. The figure below takes the first rule as an example to illustrate the two functions of url parsing and url creation. For each routing rule, CUrlManager will create a CUrlRule object to handle the two functions corresponding to this rule, so if there are several rules, there will be several CUrlRule objects. So CUrlRule is the core of url management. Let's analyze the working principle of CUrlRule.

Yii Framework Analysis (8) - URL Management Component

Each url routing rule is processed by a CUrlRule object. Next, take the following routing rule as an example: '/art////'=>'article //', describes the processing process of url parsing and url creation. The process of processing URLs by each CUrlRule object can be divided into 3 stages:
1. Initializing the CUrlRule object
In the constructor of the CUrlRule object, 6 important member variables will be initialized:

Yii Framework Analysis (8) - URL Management Component

2. Parse url
The work of parsing url is divided into three steps: a. Parse out each field in the url according to the pattern rules; b. Replace the reference fields in the route according to references. ;c. Add the fields specified in params to the $_GET array
3. Create url
The work of creating url is divided into three steps: a. According to the routePattern rules, parse out each field in the input route; b , merge the input parameter array and the array parsed in the previous step; c. Replace the template with the merged array.

Yii Framework Analysis (8) - URL Management Component

The above is the Yii framework analysis (8) - the content of the URL management component. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
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