Z
Z
ZaurK2019-05-05 16:46:53
Yii
ZaurK, 2019-05-05 16:46:53

How to setup urlManager for slugs?

Good afternoon. I'm trying to set up the urlManager so that it handles the slug, but I just can't write the rule.
When forming the url, I pass an additional slug parameter like this:

Url::to(['site/page', 'id' => $id, 'slug' => $slug]),

As a result, when you click on this link, the corresponding page opens under the url localhost:8080/page/8?slug=obrazovanie, but you need it in a more understandable form localhost:8080/page/8/obrazovanie
Here is my urlManager
'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'enableStrictParsing' => false,
            'rules' => [
                '/' => 'site/index',
                '<action:\w+>/<id:\d+>/<slug:[a-z0-9_-]+>' => 'site/<action>/<slug>',
                '<action:\w+>/<id:\d+>' => 'site/<action>',
                  '<action:\w+>' => 'site/<action>',

            ],
        ],

Tell me, please, how to do it right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
ZaurK, 2019-05-05
@ZaurK

I'm sorry, I solved the problem simply by correcting the line

'<action:\w+>/<id:\d+>/<slug:[a-z0-9_-]+>' => 'site/<action>',

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question