Answer the question
In order to leave comments, you need to log in
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',
],
],
Answer the question
In order to leave comments, you need to log in
to do this, you need to implement your own url-rules class that will convert id to the alias you need, and vice versa
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
'suffix' => '',
'rules' => [
// ...
'kontent/<id:[\w+]>'=>'action/view',
],
],
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question