Tuesday, July 3, 2012

how url reqriting works in YII

Let's review what was done and why it works. We'll start with the right part of the first rule:
'home' => 'website/index',
What is website/index exactly?
In the Yii application, each controller and its actions have corresponding internal routes. A
format for an internal route is moduleID/controllerID/actionID. For example, the
actionPage method of WebsiteController corresponds to the website/page route.
So, in order to get the controller ID, you should take its name without the Controller postfix
and make its first letter lowercased. To get an action ID, you should take action method name
without the action prefix and, again, make its first letter lowercased.
Now, what is home?
To understand it in a better way, we need to know, at least perfunctorily, what's happening
when we access our application using different URLs.
When we are using /home, URL router checks our rules one by one starting from the top trying
to match URL entered with the rule. If the match is found, then the router is getting controller
and its action from an internal route assigned to the rule and is executing it. So, /home is the
URL pattern that defines which URLs will be processed by the rule it belongs to.You can create parameterized rules using a special syntax. Let's review the third rule:
'page/<alias>' => 'website/page',
Here, we are defining an alias parameter that should be specified in URL after
/page/. It can be virtually anything and it will be passed as $alias parameter to
WebsiteController::actionPage($alias).
You can define a pattern for such a parameter. We did it for the second rule:
'<alias:about>' => 'website/page',
Alias here should match about or else, the rule will not be applied

No comments:

Post a Comment