Yii uses URL component beautification management method, Yiiurl component beautification
Examples of this article describe how Yii uses URL component beautification management. Share to everyone for your reference, as follows:
Urlmanager components
The official documentation for Yii explains the following:
urlsuffix The URL suffix used by this rule, which uses Curlmanger::urlsuffix by default and a value of NULL. For example, you can set this to. html so that the URL looks "like" as a static page.
casesensitive is case sensitive, the default is Curlmanager::casesensitive, and the value is null.
defaultparams The default get parameter used by this rule. When the rule is used to parse a request, the value of the parameter is injected into the $_get parameter.
matchvalue When a URL is created, the get parameter matches the corresponding sub-pattern. By default, Curlmanager::matchvalue is used, and the value is null.
If this property is false, it means that when the route and parameter names match the given rule, a URL is created.
If this property is true, then the given parameter programmed must match the corresponding parameter sub-pattern.
Note: Setting this property to true degrades performance.
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/
/
<title>' = ' + ' Post/read ',) </pre></title>
Call $this->createurl (' post/list ') to generate the/index.php/posts. The first rule applies.
Call $this->createurl (' Post/read ', array (' ID ' =>100)) to generate the/index.php/post/100. A second rule applies.
Call $this->createurl (' Post/read ', Array (' Year ' =>2008, ' title ' = ' A sample post ')) to generate/index.php/post/2008/a% 20sample%20post. A third rule applies.
Call $this->createurl (' Post/read ') to generate/index.php/post/read. Please note that no rules apply.
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.
Therefore, 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).
Tip: This URL is generated by the CreateURL method as a relative address. To get an absolute URL, we can use the prefix Yii:: App ()->hostinfo, or call Createabsoluteurl.
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.
Test.com/vthot want to generate test.com/vthot/
Copy the Code Code 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 One
Rule code
Copy the Code Code as follows: ' posts ' = ' post/list ',
Action Code
Copy the code as follows: Echo $this->createabsoluteurl (' post/list ');
Output
Http://localhost/test/index.php/post
Example Two
Rule code
Copy the code as follows: ' post/
' =>array (' post/show ', ' urlsuffix ' = '. html '),
Action Code
Copy the code as follows: Echo $this->createabsoluteurl (' post/show ', array (' ID ' =>998, ' name ' = ' 123 '));
Output
Http://localhost/test/index.php/post/998.html?name=123
Example Three
Rule code:
Copy the code as follows: ' post/
/
' =>array (' Post/view ', ' urlsuffix ' = '. xml '),
Action Code
Copy the code as follows: Echo $this->createabsoluteurl (' Post/view ', array (' ID ' =>998, ' mid ' = ' tody '));
Output
Http://localhost/test/index.php/post/998/tody.xml
Example Four
Rule code
Copy the Code Code as follows: ' HTTP
://. Vt.com/<_c (Look|seek) > ' =>array (' <_c>/host ', ' urlsuffix ' and ' = '). 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) Public Function actionupdate ($id) This does not support the multi-primary key, will check the exact get inside there is no ID, no ID directly do not allow access
' sayhello/'
= ' Post/hello ', name is the parameter of Postcontroller Actionhello ($name) ' post/' = ' Post/view ', domain/post/e lowercase where: The preceding alias is the parameter of the 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
Also, we can do a further cleanup of our URLs by hiding the index.php portal script in the URL. This requires that we configure the Web server, as well as the 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. Turn on rewrite
Simply put, in the main.php simple set Urlmanager, and then said 3 rules, basically covered. Finally, hide the index.php, remember. Htaccess is located in the index.php sibling directory, not the protected/directory. The rest is simple.
It is hoped that this article is helpful to the PHP program design based on YII framework.
Articles you may be interested in:
- Yiiframework Introductory Knowledge Point Summary (graphic tutorial)
- The directory structure, portal files, and routing settings for the Yii introductory tutorial
- Yii Installation and Hello World
- YII PHP Framework Practical Introductory Tutorial (detailed)
- Yii Query Builder Usage Example tutorial
- Yii implementation of single-user blog system article details page Insert Comment form method
- The method of Cgridview implementation in Yii for batch deletion
- Yii Quick Start Classic tutorial
http://www.bkjia.com/PHPjc/1085880.html www.bkjia.com true http://www.bkjia.com/PHPjc/1085880.html techarticle yii uses the URL component to beautify the management method, Yiiurl the component beautification This article describes how Yii uses URL component beautification management. Share to everyone for your reference, as follows: Urlmana ...