YII2 sef网址构建

huangapple go评论59阅读模式
英文:

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

&#39;urlManager&#39; =&gt; [
 &#39;enablePrettyUrl&#39; =&gt; true,
 &#39;showScriptName&#39; =&gt; false,
 &#39;enableStrictParsing&#39; =&gt; true,
 &#39;rules&#39; =&gt; [
  &#39;adv/&lt;id:\d+&gt;&#39; =&gt; &#39;site/adv&#39;,
  &#39;&lt;alias:[\w-]+&gt;&#39; =&gt; &#39;site/&lt;alias&gt;&#39;,
 ],
],

and there is no result.

I tried another variant in rules block:

&#39;rules&#39; =&gt; [
  &#39;adv/&lt;id:\d+&gt;&#39; =&gt; &#39;adv&#39;,
  &#39;&lt;alias:[\w-]+&gt;&#39; =&gt; &#39;site/&lt;alias&gt;&#39;,
 ],

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.

    &#39;rules&#39; =&gt; [
  &#39;adv/&lt;id:\d+&gt;&#39; =&gt; &#39;adv/&lt;add some action here&gt;&#39;,
  &#39;&lt;alias:[\w-]+&gt;&#39; =&gt; &#39;site/&lt;alias&gt;&#39;,
 ],

答案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:

&#39;urlManager&#39; =&gt; [
    &#39;enablePrettyUrl&#39; =&gt; true,
    &#39;showScriptName&#39; =&gt; false,
    &#39;rules&#39; =&gt; [
      &#39;adv/&lt;id:\d+&gt;&#39; =&gt; &#39;site/adv&#39;,
      &#39;&lt;alias:[\w-]+&gt;&#39; =&gt; &#39;site/&lt;alias&gt;&#39;,
      &#39;&lt;controller:\w+&gt;/&lt;action:\w+&gt;/&#39; =&gt; &#39;&lt;controller&gt;/&lt;action&gt;&#39;,
    ],

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([&#39;adv&#39;,&#39;id&#39; =&gt; $ladvert-&gt;id]))

and in siteController action is

actionAdv($id)

Thanx.

huangapple
  • 本文由 发表于 2023年2月6日 13:48:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75357738.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定