PHP Development Framework YII Framework Tutorial (5) URL Management

Source: Internet
Author: User
Tags chr regular expression yii

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 controller base class Ccomponent, The third is to define the cHTML help class used by view. This chapter is about Urlmanager,url management.

Return Array (     
    ... ')     
    Components ' =>array ('     
        urlmanager ' =>array ('     
            urlformat ' => ' path '),     
            ' Rules ' =>array (     
                ' Game/guess/<g:\w> ' => ' game/guess ',),),)     
;

Web application complete URL management consists of two aspects. First, when the user requests the agreed URL, the application needs to parse it into an understandable parameter. Second, application requirements provide a way to create URLs so that the URL applications created are understandable. For YII applications, these are accomplished through Curlmanager.

When using the path format URL, we can specify some URL rules to make our URLs more user-friendly. For example, we can produce a short url/post/100, rather than a lengthy/index.php/post/read/id/100. URL creation and resolution are all assigned URL rules through Curlmanager.

To specify the URL rule, we must set the attribute rules for Urlmanager application components:

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 corresponding to a single rule. The format of the route (route) must be a valid regular expression, without delimiters and modifiers. It is the path information section used to match the URL. And route should point to an effective routing controller.

A rule can bind a small number of get parameters. The general format of the parameters is as follows:

<ParamName:ParamPattern>

ParamName represents a Get parameter name, and an option Parampattern represents a regular expression that will be used to match a get parameter value. When a URL is generated, the 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 rules of Web site work. We assume that our rules include the following three:

Array (
    ' posts ' => ' post/list ', '
    post/<id:\d+> ' => ' post/read ', ' post/<year:\d{4}>
    ') /<title> ' => ' post/read ',
)

Call $this->createurl (' post/list ') to generate/index.php/posts.    The first rule applies. Call $this->createurl (' Post/read ', array (' ID ' =>100)) to generate/index.php/post/100.    The second rule applies. Invoke $this->createurl (' Post/read ', Array (' Year ' =>2008, ' title ' => ' A sample post ') to generate/index.php/post/2008/a% 20sample%20post.    The third rule applies. Call $this->createurl (' Post/read ') to produce/index.php/post/read. Please note that no rules apply.

In summary, when using CreateURL to generate URLs, the route 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 is passed to CreateURL, and if the route's rules also match the route parameters, the rule will be used to generate the URL.

If the get parameter passed to CreateURL is one of the required rules, the 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. In order for these additional parameters to appear as part of the path information, we should append/* to the rule. So, the rule post/<id:\d+>/*, 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 resolve 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 the application. This is because when parsing the requested URL, [Curlmanager] attempts to match it with each rule until a rule can be applied. Therefore, the high Flow website application should minimize its use of the URL rules.

Look at the rules used in Hangman ' game/guess/<g:\w> ' => ' game/guess ',

That is, all the similar/game/guess/xx are mapped to the Actionguess method of Game/guess, which is gamecontroller, to pass in the Get parameter in g= ' x ' mode. 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 the Gamecontroller method to call actionguess based on the Urlmanager matching rule defined by main.php, passing in the Get parameter. This allows the value of this parameter to be accessed by $_get[' G ' in actionguess.

Check to guessed correctly if     
(Isset ($_get[' G '][0]) && ($result = $this->guess ($_get[ ' G '][0])!==null)     
    $this->render ($result? ' Win ': ' Lose ');     
else///the letter are guessed correctly, but not win yet     
{     
    $guessed = $this->getpagestate (' guessed ', Array ()); c6/> $guessed [$_get[' G '][0]]=true;     
    $this->setpagestate (' guessed ', $guessed, Array ());     
    $this->render (' guess ');     
}

Using Urlmanager also allows custom rules, or hides index.php, to refer to the Yii development documentation: Http://www.yiiframework.com/doc/guide/1.1/zh_cn/topics.url

See a full set of tutorials: http://www.bianceng.cn/webkf/PHP/201301/35265.htm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.