dongxi3859 2015-02-26 23:21
浏览 25
已采纳

CakePHP路由正则表达式无法正确匹配

After updating my CakePHP version from 2.2.2 to 2.6.2, one of my routes stopped working properly.

Router::connect('/articles/:keywords', array('action' => 'search', 'controller' => 'Articles', 'keywords' => null), array('pass' => array('keywords'), 'keywords' => '[A-Za-z0-9\+_]+'));

It takes input such as "World" and "World+News" through a url such as website.com/articles/World+News and passes whatever is after articles/ to the search function in the Articles controller. This was working fine up until the update. Now it will pass up the route and go to my "cannot find route" route if there is anything other than alphanumeric characters. It's like the regex isn't matching properly. e.g. "World" and "World123" will work but "World+News" will not.

Things I have tried:

  1. Changing the regex to .* just to see if it works. It does.
  2. Changing the route from :keywords to * just to see if it works. It does.
  3. Trying something I know will fail such as excluding anything with letters in the match. It fails to use this route as expected.

I've been scouring everywhere, trying all sorts of regex combinations (the ones I have match successfully in the tester), and just generally trying to find out why this route will match but I cannot. This was working fine before the update and I can't find anything in the CakePHP documentation that would suggest why this isn't working. As far as I know the expressions have been right and I have confirmed that they fully match using a regex tester. Any help would be appreciated, thanks!

  • 写回答

1条回答 默认 最新

  • dongye6377 2015-02-27 07:59
    关注

    Actually the problem is the regex, at least it's part of the problem.

    In earlier versions, CakePHP passed the raw URL into the matching subject, which however was rather problematic, as it could require very ugly regexes, especially for non-ASCII characters. Now the URL decoded variant is being passed:

    https://github.com/cakephp/.../commit/d5283af818b59c5d96355d6e42bbd77e1322d8cb

    So since + has a special meaning in URL encoding and actually stands for a whitespace, your regex won't match anymore. It's rather easy to fix, just match a whitespace instead of the +.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?