Search code examples
phpurl-routingyii2yii-url-manager

Yii2 url routing to get a string parameter


I have a url like this

http://localhost/yii2/category/my-custom-string-parameter

I need to get the text my-custom-string-parameter

And I setup the rules like this

'urlManager' => [
            'class' => 'yii\web\UrlManager',
            'showScriptName' => false,
            'enablePrettyUrl' => true,
            'rules' => array(
                    '<controller:\w+>/<id:\d+>' => '<controller>/view',
                    '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
                    '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
                    'category/<url:>' => 'category/view',
            ),
        ],

This always give me 404 error. What should I do?


Solution

  • replace 'category/<url:>' => 'category/view',, with 'category/<id:\w+>' => 'category/view'

    Routing to View need an id and to use a string use w+ not url: