如何在Yii2中重写网址。我想重写网址
/后/索引?ID = 1
到
/后/ 1-示例
答案 0 :(得分:1)
您需要在rules
(如果是高级模板)或urlManager
中为config/main.php
添加以下web.php
代码。
[
'components' => [
'urlManager' => [
'showScriptName' => false,
'enablePrettyUrl' => true,
'enableStrictParsing' => false,
'rules' => [
'post/<id:\d+>-example' => 'post/index',
],
],
],
]
答案 1 :(得分:0)
[
'components' => [
'urlManager' => [
'showScriptName' => false,
'enablePrettyUrl' => true,
'enableStrictParsing' => false,
'rules' => [
'post/<id:\d+>-<title:\w+>' => 'post/index',
],
],
],
]
答案 2 :(得分:0)
您需要在代码部分下的backend/config/main.php
添加以下代码:
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',),
],
我发布的仅仅是因为ians应该是所有整个项目的通用。