Yii2 UrlManager Breaks On Periods

泪湿孤枕 提交于 2020-02-03 01:37:08

问题


I am currently using yii2 urlManager to create a pretty URL. It receives a parameter and then looks up users based on it. Unfortunately, if the parameter contains a period, it won't run at all and redirects to a 404 page.

So,

mysite.com/me/jbroad 

works perfectly, but

mysite.com/me/j.broad 

returns a 404 page.

Here is my url manager code

'urlManager' => [
     'enablePrettyUrl' => true,
     'showScriptName' => false,
     'rules' => [
         '' => 'site/index',
         'me/<id:\w+>' => 'kit/page',
         'generate/<id:\w+>' => 'kit/generate',
          '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
     ],
],

回答1:


You are using the regex with token \w+ which matches any word character (equal to [a-zA-Z0-9_]) and does not include a period or ..

so change it to [\w\.]+ and your rule will now be

me/<id:[\w\.]+>' => 'kit/page',



回答2:


Instead of \w+ , put [-a-zA-Z0-9_.]+

example:

    'me/<id:[-a-zA-Z0-9_.]+>' => 'kit/page',


来源:https://stackoverflow.com/questions/55522404/yii2-urlmanager-breaks-on-periods

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!