3

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?

1 Answer 1

6

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:

Sign up to request clarification or add additional context in comments.

1 Comment

Actually the part before the ":" is just the name that will be used to store the value in the GET variables. "url" is perfectly fine if that is a better name than "id". The regex behind the ":" is what needs to match and the OPs code didn't work because the regex was missing completely

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.