英文:
YII2 sef urls construction
问题
我有一个包含产品的目录。 产品的网址看起来像这样 domain.com/adv?id=14792
。
我想要美化网址,使其像这样 domain.com/adv/14792
。
在 web.php 中,我尝试了以下方式:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => true,
'rules' => [
'adv/<id:\d+>' => 'site/adv',
'<alias:[\w-]+>' => 'site/<alias>',
],
],
但没有结果。
我尝试了另一种变体在规则块中:
'rules' => [
'adv/<id:\d+>' => 'adv',
'<alias:[\w-]+>' => 'site/<alias>',
],
这样网址开始看起来像我想要的。但是在这个链接上,我得到了404页面。
英文:
I have catalogue with products. Urls for products looks like this domain.com/adv?id=14792
.
I want to beautify urls like this domain.com/adv/14792
.
In web.php I tried to like this
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => true,
'rules' => [
'adv/<id:\d+>' => 'site/adv',
'<alias:[\w-]+>' => 'site/<alias>',
],
],
and there is no result.
I tried another variant in rules block:
'rules' => [
'adv/<id:\d+>' => 'adv',
'<alias:[\w-]+>' => 'site/<alias>',
],
and urls start to look like I want. But on this link I get 404 page.
答案1
得分: 0
请使用带有动作和副词以使其起作用。
'rules' => [
'adv/<id:\d+>' => 'adv/<在此添加动作>',
'<alias:[\w-]+>' => 'site/<alias>',
],
英文:
Please use action with adv so that it work.
'rules' => [
'adv/<id:\d+>' => 'adv/<add some action here>',
'<alias:[\w-]+>' => 'site/<alias>',
],
答案2
得分: 0
I solved my problem by myself. This is my urlmanager module now:
'urlManager
' => [
'enablePrettyUrl
' => true,
'showScriptName
' => false,
'rules
' => [
'adv/<id:\d+>
' => 'site/adv
',
'<alias:[\w-]+>
' => 'site/<alias>
',
'<controller:\w+>/<action:\w+>/
' => '<controller>/<action>
',
],
]
this construction build url from https://example.com/adv?id=123456
to https://example.com/adv/123456
.
In view link build with
Html::a($insideEl, Url::to(['adv', 'id' => $ladvert->id]))
and in siteController action is
actionAdv($id)
Thanx.
英文:
I solved my problem by myself. This is my urlmanager module now:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'adv/<id:\d+>' => 'site/adv',
'<alias:[\w-]+>' => 'site/<alias>',
'<controller:\w+>/<action:\w+>/' => '<controller>/<action>',
],
this construction build url from https://example.com/adv?id=123456
to https://example.com/adv/123456
.
In view link build with
Html::a($insideEl,Url::to(['adv','id' => $ladvert->id]))
and in siteController action is
actionAdv($id)
Thanx.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论