O Guia Definitivo do Yii 1.1
O Guia Definitivo do Yii 1.1
O Guia Definitivo do Yii 1.1
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
array(<br />
'posts'=>'post/list',<br />
'post/'=>'post/read',<br />
'post//'=>'post/read',<br />
)<br />
• Calling $this->createUrl('post/list') generates /index.php/posts. The first rule is<br />
applied.<br />
• Calling $this->createUrl('post/read',array('id'=>100)) generates /index.php/post/100.<br />
The second rule is applied.<br />
• Calling $this->createUrl('post/read',array('year'=>2008,'title'=>'a sample post'))<br />
generates /index.php/post/2008/a%20sample%20post. The third rule is applied.<br />
• Calling $this->createUrl('post/read') generates /index.php/post/read. None of the rules<br />
is applied.<br />
In summary, when using createUrl to generate a URL, the route and the GET parameters<br />
passed to the method are used to decide which URL rule to be applied. If every parameter<br />
associated with a rule can be found in the GET parameters passed to createUrl, and if the<br />
route of the rule also matches the route parameter, the rule will be used to generate the<br />
URL.<br />
If the GET parameters passed to createUrl are more than those required by a rule, the<br />
additional parameters will appear in the query string. For example, if we call $this-<br />
>createUrl('post/read',array('id'=>100,'year'=>2008)), we would obtain /index.php/post/<br />
100?year=2008. In order to make these additional parameters appear in the path info part,<br />
we should append /*to the rule. Therefore, with the rule post//*, we can obtain the<br />
URL as/index.php/post/100/year/2008.<br />
As we mentioned, the other purpose of URL rules is to parse the requesting URLs.<br />
Naturally, this is an inverse process of URL creation. For example, when a user requests<br />
for /index.php/post/100, the second rule in the above example will apply, which resolves in<br />
the route post/read and the GET parameter array('id'=>100) (accessible via $_GET).<br />
Note: Using URL rules will degrade application performance. This is because when<br />
parsing the request URL, CUrlManager will attempt to match it with each rule until one<br />
can be applied. The more the number of rules, the more the performance impact.<br />
Therefore, a high-traffic Web application should minimize its use of URL rules.<br />
Parameterizing Routes<br />
Starting from version 1.0.5, we may reference named parameters in the route part of a<br />
rule. This allows a rule to be applied to multiple routes based on matching criteria. It may<br />
also help reduce the number of rules needed for an application, and thus improve the<br />
overall performance.<br />
We use the following example rules to illustrate how to parameterize routes with named<br />
parameters: