YII uses the url component beautification management method, and the yiiurl component beautification. YII uses the url component beautification management method. the yiiurl component beautification example describes how YII uses the url component beautification management method. For your reference, see url component beautification management in urlMana YII.
This article describes how to beautify the management of YII using url components. 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>
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.
Articles you may be interested in:
- YiiFramework knowledge point summary (graphic tutorial)
- Directory structure, entry file, and route settings in Yii Getting Started Tutorial
- Yii installation and hello world
- Yii PHP Framework practical Getting Started Tutorial (details)
- Yii Query Builder usage example tutorial
- Yii: how to insert a comment form to the document details page of a single-user blog system
- Batch delete of CGridView in Yii
- Yii Quick Start Classic Tutorial
Examples in this article describes how to beautify the management of YII using url components. For your reference, urlMana...