E
E
Evgeny Khakhalkin2017-03-21 23:25:10
Yii
Evgeny Khakhalkin, 2017-03-21 23:25:10

How to change ID to text in Yii2 url?

Good day.
The problem, in general, is this: The
content of the page is loaded by ID
test.loc/kontent/3
I want to make it so that instead of ID there is a CNC text, for example
test.loc/kontent/novost
Please tell me how this can be implemented in yii2?

'urlManager' => [
            'class' => 'yii\web\UrlManager',
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'enableStrictParsing' => false,
            'suffix' => '',
            'rules' => [
                ''=>'site/index',
                'news'=>'news/index',
                'articles'=>'articles/index',                
                'contact-us'=>'site/contact',                
                'comments'=>'comment/index',
                'kontent'=>'kontent/index',                
                'kontent/<id:\d+>'=>'action/view',            
            ],
        ],

Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Fedorov, 2017-03-21
@qonand

to do this, you need to implement your own url-rules class that will convert id to the alias you need, and vice versa

M
Mikhail Serenkov, 2017-03-21
@miserenkov

'urlManager' => [
            'class' => 'yii\web\UrlManager',
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'enableStrictParsing' => false,
            'suffix' => '',
            'rules' => [
                // ...            
                'kontent/<id:[\w+]>'=>'action/view',            
            ],
        ],

And when generating the url, instead of id, substitute the desired alias

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question