My yii PHP project has UserController and it has an action called actionView. I can access user view page using following URL
mysite.com/user/view/id/1
I want to change that one to
mysite.com/username
How Can I do it.
I know that i can simply create rule to be more user friendly to get url such as
mysite.com/user/username
But url scheme with database resource name as direct param (mysite.com/username
) is whole different story.
Url rule:
Note that in such scheme, you must also create rules for all your controllers and place above rule at the end, so better prefix it with user:
Resulting url will be
example.com/user/username
In action:
Update: To make rule which reacts on any input variable create custom url rule class, here is some example, modify to your needs:
Then place this rule in beginning of urlmanager config
Important: If user has username same as your controller, it will match username and controller will be inaccessible. You must forbid registering users with same names as controllers.