Reputation: 1160
I'm trying to hide blog id
from url.
For understanding better:
I want this URL
mysite/blog/post-slug-<id> // will be like: mysite/blog/post-slug-358
change to
mysite/blog/post-slug
Here my my URL code:
<a href="<?= Url::to(['post/view',
'id' => $model->id, 'slug' => $model->slug ]) ?>">
and this is my config/main:
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'blog/<slug>-<id>' => 'blog/view',
]
could any one help me to solve it?
Upvotes: 0
Views: 416
Reputation: 577
Remove -<id>
from your url manager:
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'blog/<slug>' => 'blog/view',
]
]
Then in your BlogController in actionView you can get it with this code:
$slug = Yii::$app->getRequest()->getQueryParam('slug');
I'm not sure that 'class' is nessecary; This i in my project how I use it:
'urlManager' => [
'baseUrl' => '/',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'ajax/<action>' => 'ajax/<action>',
'<first_step>/<second_step>/<third_step>' => 'page/index',
'<first_step>/<second_step>' => 'page/index',
'<first_step>' => 'page/index',
'<first_step:.+/>' => 'page/index', // redirect 301 /
],
],
Upvotes: 1