A concise tutorial on the development of Yii Framework (4) Hangman guess the word game example omitted several aspects of the problem, one is the configuration file main.php Urlmanager, the second is the Controller base class Ccomponent, The third is to define the cHTML helper class that the view uses. This chapter introduces Urlmanager,url management of the war.
Return Array (... ' Components ' =>array (' Urlmanager ' =>array (' urlformat ' = ' path ', ' Rules ' =>array (' game/ guess/' = ' game/guess ',),),);
The full URL management of a Web application consists of two facets. First, when the user requests the URL of the contract, the application needs to parse it into an understandable parameter. Second, application requirements provide a way to create URLs in order to create URLs that the application can understand. For YII applications, these are done through curlmanager assistance.
When using the path format URL, we can specify certain URL rules to make our URLs more user-friendly. For example, we can produce a short url/post/100, not a lengthy/index.php/post/read/id/100. URL creation and parsing are all specified by Curlmanager URL rules.
To specify the URL rule, we must set the properties of the Urlmanager application symbol rules:
Array (...) ' Components ' =>array (... ' Urlmanager ' =>array (' urlformat ' = ' path ', ' Rules ' =>array (' Pattern1 ' = ' = ' route1 ', ' pattern2 ' = ' route2 ', ' pattern3 ' = ' route3 ', '),),);
These rules are specified in a series of route formats, each pair corresponds to a single rule. The route format must be a valid regular expression, with no delimiters and modifiers. It is a part of the path information used to match URLs. And the route should point to a valid route controller.
A rule can bind a small number of get parameters. The general format of the parameters is as follows:
ParamName represents the Get parameter name, and an option Parampattern represents 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, and when a URL is parsed, the corresponding get parameters are generated by parsing the result.
We use some examples to explain the Web site work rules. We assume that our rules include the following three:
Array (' posts ' = ' post/list ', ' post/' = ' post/read ', ' post//
In summary, when using CreateURL to generate URLs, alignments and the get parameters passed to the 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, and if the alignment rules also match the alignment parameters, the rule will be used to generate the URL.
If the get parameter is passed to CreateURL is one of the rules required above, additional 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. In order for these additional parameters to appear as part of the path information, we should append/* to the rule. So, the rule post//*, we can get URL/index.php/post/100/year/2008.
As we mentioned, the other purpose of the URL rule is to parse the request URL. Of course, this is a reverse process of URL generation. For example, when a user requests/index.php/post/100, the second rule of the above example will apply to parse the route post/read and get parameter array (' ID ' =>100) (available through $_get).
The CreateURL method produces a relative address. In order to get an absolute URL, we can use the prefix yii ">
Note: The URL rules used will degrade the performance of your app. This is because when the URL of the request is parsed, [Curlmanager] attempts to match it with each rule until a rule can be applied. Therefore, high-traffic site applications should minimize the URL rules they use.
Take a look at the rules used in hangman ' game/guess/' = ' game/guess ',
That is, all similar/game/guess/xx are mapped to the Actionguess method of game/guess that is Gamecontroller, and the get parameters are passed in the way of g= ' X '. Refer to each letter link
Chtml::linkbutton (Chr ($i), Array (' Submit ' =>array (' guess ', ' G ' =>chr ($i)));
Click on the letter link for/game/guess/?g=x or/game/guess/x according to main.php definition of the urlmanager matching rule the Yii framework calls Gamecontroller Actionguess method, passing in the Get parameter. This allows you to access the value of this parameter in actionguess by $_get[' G '].
Check to see if the guessed Correctlyif (Isset ($_get[' G '][0]) && ($result = $this->guess ($_get[' g '] [ 0])!==null) $this->render ($result? ' Win ': ' lose '); else//the guessed correctly, but not win yet{$guessed = $this->getpagestate (' guessed ', Array () ); $guessed [$_get[' G '][0]]=true; $this->setpagestate (' guessed ', $guessed, Array ()); $this->render (' guess ');} Using Urlmanager also allows you to customize rules, or hide index.php.
The above is the PHP Development Framework Yii Framework Tutorial (5) URL management content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!