Can we display url in the format controller/action/id
to /id
in Yii? Also, one major problem is that I need to do this only on the specific controller/action.
For Instance: If I have /user/post/ujjwal
then I need to show /ujjwal
only. And when I have /image/display/ujjwal
then it can be as it is?
I have read the url management in Yii and understand whatever is shown in there. But I dont' know how to control the url based on the controller/action
.
Any help would be appreciated.
Thanks,
Ujjwal
In your config/main.php
try
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'user/post/<user:\w+>'=>'<user>', //this should be the very first rule
//other rules
),
),
),
In my opinion you can not map one customized URL to two or more URL path or action. Like when someone access /my_username
the will be redirected to /user/post/ujjwal
or /image/display/ujjwal
. The application will be confused because of this pattern/rules.
So I recommend you to change the /image/display/ujjwal
to another pattern/rules. Like /my_username/image
I use username
as the query string for selected username.
'components'=>array(
.....
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'<username:[\w.]+>'=>'user/post',
'<username:[\w.]+>/display'=>'image/display',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
....
Someone has asked this similar question before: How to specify a SEO friendly url like twitter www.twitter.com/<name> using YII framework
What you want can be done, with a few side effects however. If you put this in your config/main.php
:
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'image/<action>/<username>' => 'image/action/username',
'<username>' => 'user/post/username',
),
),
Put all your more specific rules first, then everything else that is a single unit will go to /user/post/something
.