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( '/art/<cate:\w+>/<key:\d+>/<id:\d+>/<p:\d+>'=>'article/<cate>/<key>', 'post/<id:\d+>/<title:.*?>'=>'post/view', '<controller:\w+>/'=>'<controller>/', ), ), );</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.
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:
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.
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)!