[英]regex matching url
在PHP中,klein路由将匹配尽可能多的路由。 我设置的2条路线存在冲突。 他们是:
$route1: '/websites/[i:websiteId]/users/[i:id]?'
和
$route2: '/websites/[i:websiteId]/users/[a:filename].[json|csv:extension]?'
这是我要匹配的URL,我认为应该匹配第一个而不是第二个:
/api/v1-test/websites/100/users/4
为这两个生成的正则表达式为:
$regex1: `^/api(?:/(v1|v1-test))/websites(?:/(?P<websiteId>[0-9]++))/users(?:/(?P<id>[0-9]++))?$`
$regex2: `^/api(?:/(v1|v1-test))/websites(?:/(?P<websiteId>[0-9]++))/users(?:/(?P<filename>[0-9A-Za-z]++))(?:\.(?P<extension>json|csv))?$`
我的意思是,如果没有“ .csv”或“ .json”,它就不匹配。 问题在于它同时匹配这两个路由。 第二个结果文件名是“ 4”,扩展名是空白。
发送/api/v1-test/websites/100/users/users.csv可以正常工作,并且仅与第二条路由匹配。
我只能控制路由,而不能控制正则表达式或匹配项。 谢谢。
这一点
(?:\.(?P<extension>json|csv))?
在第二个正则表达式的末尾,使它匹配是否由于文件名而导致文件名?
在最后。 问号表示0 or 1 of the previous expression
。 摆脱它,至少,字符串只有在具有扩展名时才匹配此正则表达式。
要进行此更改,只需从第二条路线中删除问号,如下所示:
$route2: '/websites/[i:websiteId]/users/[a:filename].[json|csv:extension]'
问题是match_type
确实定义了……很奇怪:
$match_types = array(
'i' => '[0-9]++',
'a' => '[0-9A-Za-z]++',
[...]
因此,您无法真正捕获仅与[a-zA-Z]
对应的序列...我看到的唯一选择是使用3条路线:
$route1: '/websites/[i:websiteId]/users/[i:id]?'
$route2: '/websites/[i:websiteId]/users/[a:filename]'
$route3: '/websites/[i:websiteId]/users/[a:filename].[json|csv:extension]'
并为路由2和3分配相同的操作,那么您将获得:
/api/v1-test/websites/100/users/
匹配为1 /api/v1-test/websites/100/users/4
由1匹配 /api/v1-test/websites/100/users/test
由2匹配 /api/v1-test/websites/100/users/test.csv
由3匹配 这似乎是您想要的行为。
另一个(更简单)的解决方案是利用文档的以下内容:
Routes automatically match the entire request URI. If you need to match only a part of the request URI or use a custom regular expression, use the @ operator.
然后,您可以像这样定义路线:
$route1: '@/websites/[0-9]+/users/[0-9]*$'
$route1: '@/websites/[0-9]+/users/[a-zA-Z]+(\.[a-zA-Z]+)?$'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.