YII method of beautifying management using url components _ php instance

Source: Internet
Author: User
This article mainly introduces the method of using url component beautification Management in YII, and analyzes the specific functions and related usage skills of the urlManager component in detail based on the instance form, for more information about how to use url components in YII, see the following example. We will share this with you for your reference. The details are as follows:

UrlManager component

Yii official documentation explains this as follows:

UrlSuffixThe url suffix used by this rule. The default value is CurlManger: urlSuffix. The value is null. For example, you can set this to .html to make the url look like a static page.
CaseSensitiveWhether it is case sensitive. The default value is CUrlManager: caseSensitive. The value is null.
DefaultParamsThe default get parameter used by this rule. When this rule is used to parse a request, the value of this parameter is injected into the $ _ GET parameter.
MatchValueWhen a URL is created, the GET parameter matches the corresponding sub-mode. By default, CurlManager: matchValue is used. The value is null.

If this attribute is set to false, a URL is created when the route and parameter name match the specified rule.

If this attribute is true, the specified parameter value must match the corresponding parameter sub-mode.

Note: setting this attribute to true reduces performance.

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/
  
   
/
   '=> 'Post/read',) </pre> </p>
   

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.

Call $ this-> createUrl ('Post/read', array ('Year' => 2008, 'title' => 'a sample Post') to generate/index. php/post/2008/a % 20 sample % 20 post. The third rule applies.

Call $ this-> createUrl ('Post/read') to generate/index. php/post/read. Note that no rules apply.

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 a 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, this rule post/ /*. You 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 a user requests/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) (can be obtained through $ _ GET ).

Tip: The createurl method generates a relative address. To obtain an absolute url, we can use the prefix yii: app ()-> hostInfo or call createAbsoluteUrl.

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.

Test.com/vthot to generate test.com/vthot/

The Code is as follows:

'Urlsuffix '=> '/',


To change the URL format, we should configure the urlManager application component so that createUrl can automatically switch to the new format and the application can correctly understand the new URL:

'urlManager'=>array(  'urlFormat'=>'path',  'showScriptName'=>false,  'urlSuffix'=>'.html',  'rules'=>array(    'posts'=>'post/list',    'post/
    
     '=>array('post/show','urlSuffix'=>'.html'),    'post/
     
      /
      
       '=>array('post/view','urlSuffix'=>'.xml'),  ),),
      
     
    

Example 1

Rule code

The Code is as follows:

'Posts' => 'Post/list ',


Action Code

The Code is as follows:

Echo $ this-> createAbsoluteUrl ('Post/list ');

Output

Http: // localhost/test/index. php/post

Example 2

Rule code

The Code is as follows:

'Post/ '=> Array ('Post/show', 'urlsuffix '=> '.html '),


Action Code

The Code is as follows:

Echo $ this-> createAbsoluteUrl ('Post/show ', array ('id' => 998, 'name' => '123 '));

Output

Http: // localhost/test/index. php/post/998.html? Name = 123

Example 3

Rule code:

The Code is as follows:

'Post/ / '=> Array ('Post/view', 'urlsuffix' => '. xml '),

Action Code

The Code is as follows:

Echo $ this-> createAbsoluteUrl ('Post/view', array ('id' => 998, 'mid '=> 'tody '));


Output

Http: // localhost/test/index. php/post/998/tody. xml

Example 4

Rule code

The Code is as follows:

'Http :// .Vt.com/<_ c :( look | seek)> '=> array (' <_ c>/host', 'urlsuffix '=>'. me '),

Action Code:

echo $this->createAbsoluteUrl('look/host',array('user'=>'boy','mid'=>'ny-01'));echo '';echo $this->createAbsoluteUrl('looks/host',array('user'=>'boy','mid'=>'ny-01'));

Output

Http://boy.vt.com/look.me? Mid = ny-01
Http: // localhost/test/index. php/looks/host/user/boy/mid/ny-01

1) controller/Update/id/23

Public function actionUpdate () {$ id = Yii: app ()-> request-> getQuery ('id '); processed $ _ GET ['id']} // $ id = Yii: app ()-> request-> getPost ('id '); processed $ _ POST ['id'] // $ id = Yii: app ()-> request-> getParam ('id'); // CHttpRequest more

2) The public function actionUpdate ($ id) does not support multiple primary keys. It checks whether there is an id in GET. Without an id, access is not allowed directly.

'Sayhello/
    
     
'=> 'Post/hello', name is the PostController actionHello ($ name) parameter 'Post/' => 'Post/view', where domain/post/e is in lowercase: the preceding alias parameter is PostController actionView ($ alias) '(posts | archive )/
     
      
'=> 'Post/Index', domain/posts/DESC or domain/posts/ASC' (posts | archive) '=> 'Post/Index ', domain/posts or domain/archive 'tos '=> array ('website/page', 'defaultparams' => array ('Alias' => 'terms _ of_service ')),
     
    

When the URL is/tos, pass terms_of_service as the alias parameter value.

Hide index. php

Another point is that we can further clean up our website by hiding the index. php entry script in the URL. This requires us to configure the Web server and urlManager application components.

1. add showScriptName => false

2. add project/. htaccess

RewriteEngine on# if a directory or a file exists, use it directlyRewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-d# otherwise forward it to index.phpRewriteRule . index.php

3. Enable rewrite

To put it simply, set urlManager in main. php and introduce three rules, which are basically covered. The last step is to hide index. php. Remember that. htaccess is located in the same directory as index. php, rather than the protected/directory. Others are simple.

I hope this article will help you design PHP programs based on the Yii framework.

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.