In the previous concise YiiFramework development tutorial (4) Hangman guessed word game instances skipped several issues. The first was the configuration file main. the URLManager of php, the base class CComponent of Controller, and the CHtml help class used by View. This article introduces URLManager and URL management. In the previous concise Yii Framework Development Tutorial (4) Hangman guessed word game instances skipped several issues. The first was the configuration file main. the URLManager of php, the base class CComponent of Controller, and the CHtml help class used by View. This article introduces URLManager and URL management.
return array(...'components'=>array('urlManager'=>array('urlFormat'=>'path','rules'=>array('game/guess/'=>'game/guess',),),),);
The complete URL management of Web applications includes two aspects. First, when a user requests a specified URL, the application needs to parse it into understandable parameters. Second, the application needs to provide a method to create a URL so that the created URL application can understand it. For Yii applications, these are done with CUrlManager assistance.
When using the path format URL, we can specify some URL rules to make our URL more user-friendly. For example, we can generate a short URL/post/100 instead of lengthy/index. php/post/read/id/100. You can use CUrlManager to specify URL rules for URL creation and resolution.
To specify the URL rule, we must set the attribute rules of the urlManager application component:
array(......'components'=>array(......'urlManager'=>array('urlFormat'=>'path','rules'=>array('pattern1'=>'route1','pattern2'=>'route2','pattern3'=>'route3',),),),);
These rules are specified for arrays in a series of route formats, and each pair corresponds to a single rule. The route format must be a valid regular expression without separators or modifiers. It is used to match the URL information. Also, route should point to a valid route controller.
A rule can bind a small number of GET parameters. The common format of parameters is as follows:
ParamName indicates the GET parameter name. The optional ParamPattern indicates the regular expression that will be used to match the GET parameter value. When a URL is generated, these parameter tokens are replaced by the corresponding parameter values. when a URL is parsed, the corresponding GET parameter is generated by parsing the result.
We use some examples to explain the website work rules. Assume that our rules include the following:
array('posts'=>'post/list','post/'=>'post/read','post//
In short, when you use createUrl to generate a URL, the route and GET parameters passed to this method are used to determine which URL rules apply. If each parameter in the association rule can be found in the GET parameter, it will be passed to createUrl. if the route rule matches the route parameter, the rule will be used to generate the URL.
If the GET parameter is passed to createUrl as a rule described above, other parameters will appear in the query string. For example, if we call $ this-> createUrl ('post/read', array ('id' => 100, 'year' => 2008 )), we will get/index. php/post/100? Year = 2008. To make these extra parameters appear in part of the path information, we should add/* to the rule /*. Therefore, for the post/* rule, we can obtain the URL/index. php/post/100/year/2008.
As we mentioned, other purposes of URL rules are to parse the request URL. Of course, this is an inverse process of URL generation. For example, when you request/index. php/post/100, the second rule in the above example will apply to parse the route post/read and GET parameters array ('id' => 100) (obtained through $ _ GET ).
The createurl method generates a relative address. To get an absolute url, we can use the prefix yii ">
Note: The URL rules used will reduce the application performance. This is because when the request URL is parsed, [CUrlManager] tries to use each rule to match it until a rule can apply. Therefore, high-traffic website applications should minimize the URL rules they use.
Let's take a look at the rules 'Game/guess/'=> 'Game/guess' used in Hangman ',
That is to say, map all the similar/game/guess/xx to game/guess, that is, the actionGuess method of GameController, and pass in the GET parameter in the g = 'X' way. Refer to the link of each letter
CHtml::linkButton(chr($i),array('submit'=>array('guess','g'=>chr($i))));
Click/game/guess /? G = x or/game/guess/x call the actionGuess method of GameController according to the URL manager matching rule Yii Framework defined by main. php and pass in the GET parameter. In this way, the value of this parameter can be accessed through $ _ GET ['G'] in actionGuess.
// Check to see if the letter is guessed correctlyif (isset ($ _ GET ['G'] [0]) & ($ result = $ this-> guess ($ _ GET ['G'] [0])! = Null) $ this-> render ($ result? 'Win': 'lose'); else // the letter is guessed correctly, but not win yet {$ guessed = $ this-> getPageState ('guessed ', array (); $ guessed [$ _ GET ['G'] [0] = true; $ this-> setPageState ('guessed ', $ guessed, array (); $ this-> render ('Guess ');} use urlManager to allow custom rules or hide indexes. php.
The above is the URL management content of the PHP development Framework Yii Framework tutorial (5). For more information, see PHP Chinese website (www.php1.cn )!