Controlling url in Yii: controller/action/id to /id

前端 未结 3 664
滥情空心
滥情空心 2021-01-22 13:02

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/

相关标签:
3条回答
  • 2021-01-22 13:36

    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.

    0 讨论(0)
  • 2021-01-22 13:43

    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

    0 讨论(0)
  • 2021-01-22 13:46

    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 
                             ),
                ),
            ),
    
    0 讨论(0)
提交回复
热议问题