【问题标题】:Yii2 URL Route for make a Slug UrlYii2 URL Route 用于制作 Slug Url
【发布时间】:2016-08-28 13:44:25
【问题描述】:

我在yii2上学习, 制作 url 路由时遇到问题。 这是我的网址配置

'urlManager' => [
            'class' => 'yii\web\UrlManager',
            // Disable index.php
            'showScriptName' => false,
            // Disable r= routes
            // 'enableStrictParsing' => false,
            'enablePrettyUrl' => true,
            'rules' => array(                   
                'category/<id:\S+>' => 'category/detail',
                '<controller:\w+>/<id:\d+>' => '<controller>/view',
                '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
                '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
                'request-password-reset' => 'site/request-password-reset',
                'reset-password/<token:\S+>' => 'site/reset-password',  
                'profile/<id:[0-9a-zA-Z\-]+>/?' => 'profile/user',  
                'logout' => 'site/logout',
                'login' => 'site/login',

            ),
        ],

问题出在配置文件路由中。 情况是这样的:

  1. http://localhost/myapps/profile/some-url/ -> 使用添加的斜线 网址结束
  2. http://localhost/myapps/profile/some-url -> url 末尾没有斜杠不起作用,错误 404
  3. http://localhost/myapps/profile/someurl -> 不工作错误 404。
  4. http://localhost/myapps/profile/first-second-third -> 我们的不带斜线到底是有效的

我真的很困惑,我一直在这里寻找以前的问题,但没有尝试任何适用于我的案例。

有人可以帮我解决我的问题吗?

【问题讨论】:

    标签: php url yii2 url-routing


    【解决方案1】:

    只需将您的个人资料别名移至列表顶部即可:

    'profile/<id:[0-9a-zA-Z\-]+>/?' => 'profile/user',
    'category/<id:\S+>' => 'category/detail',  
    '<controller:\w+>/<id:\d+>' => '<controller>/view',
    '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
    '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
    'request-password-reset' => 'site/request-password-reset',
    'reset-password/<token:\S+>' => 'site/reset-password',  
    'logout' => 'site/logout',
    'login' => 'site/login',
    

    由于'&lt;controller:\w+&gt;/&lt;action:\w+&gt;' =&gt; '&lt;controller&gt;/&lt;action&gt;' 是在'profile/&lt;id:[0-9a-zA-Z\-]+&gt;/?' =&gt; 'profile/user' 之前定义的,因此它永远不会到达配置文件,因为它会将其识别为控制器/动作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-07
      • 2021-03-14
      • 1970-01-01
      • 1970-01-01
      • 2018-03-13
      相关资源
      最近更新 更多