Yii 2.0 urlManager

In this post I want to talk about urlManager in Yii 2.0 and how to create cleaner, and friendlier urls. For those of you who have worked with Yii 1.x, I have to say this is somehow the same. What is urlManager? By default Yii creates urls based on the following pattern:
In this post I want to talk about urlManager in Yii 2.0 and how to create cleaner, and friendlier urls.
For those of you who have worked with Yii 1.x, I have to say this is somehow the same.
What is urlManager?
By default Yii creates urls based on the following pattern:
<controller>/<action>
<controller>/<action>/<id>
So, if you have a controller named SiteController.php and in this controller you have an action named actionPosts() , by default Yii creates the first type of urls for it. So the link to this page would be examle.com/site/posts. If in the same controller you have an action named actionCategory($id), by default Yii creates the second type of urls for it. So the link to this page would be example.com/site/category/id.
Sometimes we want to have different urls. As an example we have an action in SiteController.php, named actionPage($id). As you have guessed the link to this page would be, example.com/site/page/id, but we want a cleaner url for this action such as example.com/site/id. This request and requests like this, will be handled with the help of urlManager.
urlManager is an array in which we can define the urls which are different from Yii default urlManager. You can find urlManager in web.php which is located in config folder. If you cannot find it, you can easily add it to web.php. In components array, add the following for urlManager:
'urlManager' => [
'rules' => [
'<controller:[\w-]+>/<action:[\w-]+>' => '<controller>/<action>',
'<controller:[\w-]+>/<action:[\w-]+>/<id:\d+>' => '<controller>/<action>',
],
This is your default urlManager. You can add your rules to this. :[\w-] stands for word which means, this should be a word (string) and dash character. :\d stands for digit which means this should be a number. For example '<controller:[\w-]+>/<action:[\w-]+>/<id:\d+>' => '<controller>/<action>' says that controller is a string, action is string, and id is a number. In our example this rule creates urls such as example.com/site/category/4. This goes to SiteController.php and actionCategory($id). As you can see in the example, name of the parameter sent to the action, should be the same name that we used is urlManager, for that rule. Since our rule is '<controller:[\w-]+>/<action:[\w-]+>/<id:\d+>', our action takes $id as its parameter.
Each rule has two parts. The left part shows what we enter as our url and the right part, tells Yii that this url goes to which controller and which action. You may want to point out that in the right part, we didn't write our parameter, well don't worry. When you write your parameter in the left part, Yii will automatically understand that in the right part it has a parameter as well.
So, for our example, if we want to add a rule that creates the following url and urls like that, we have to add a new rule to our urlManager.
Original Url: example.com/site/page/5
Cleaner Url: example.com/site/5
Our rule should be: 'site/<id:\d>' => 'site/page',
Now you know that if we want a rule for a specific controller and/or action, instead of writing <controller:[\w-]> and/or <action:\d> we can easily write the name of our controller and/or action.
REMEMBER: Since Yii checks the rules from top to bottom, if you think your rule can be overwritten by another rule which you have in your urlManager, write the one that is less general first. As an example, imagine you have these two rules:
'<cont
roller:[\w-]+>/list/<archive_id:\d+>' => '<controller>/archive',
and
'<control
ler:[\w-]+>/<action:[\w-]+>/<id:\d+>' => '<controller>/<action>',
As you can see, the first rule is less general, and it will be used only if we are calling list action of a controller. So we write it first. But what happens if we write it after our second rule. Well, Yii will never get to it. In this case, if your url is example.com/site/archive/5 because site is a controller, list can be an action in that controller, and 5 is an id, The second rule is correct too, and since we have written it first, yii will immidiately selects this rule and while executing the codes, Yii may encouter some errors (such as not finding the action because in our example we don't have any actions named list in our controller). However if you write the first rule first, Yii will choose it as its correct rule, so Yii will not encounter any errors, except syntax errors (if we have any).