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.