【发布时间】:2016-01-10 23:50:28
【问题描述】:
我需要检查路由文件中的有效路由,我想在其中为动态 url 部分放置通配符(或占位符)。 路由器读取该json格式的所有路由:
{"action" : "BlogController@showPost", "method" : "GET", "url" : "showPost/id/{}"}
当比较发生时,我需要将持有者 {any} 更改为当前值,并可能允许将正则表达式放在 {any} 标记内。
这样的网址: showPost/id/211 必须与 showPost/id/{} 进行比较并且应该返回 true。如果可能的话,我希望允许将 {'[0-9]\'} 作为可选参数,以确保实际值与正则表达式匹配。
什么是最好的解决方案?
比较方法是这样的:
public static function findAction($query) {
foreach (Router::getInstance()->routes as $route) {
if ($route->url == $query) {
return $route;
}
}
}
$query 包含 /showPost/id/221,Router::getInstance()->routes->route->url 包含 showPost/id/{}
这篇文章与这个自动解决的问题有关: how to make nice rewrited urls from a router 为了避免重复,我不会重新发布路由器代码。
提前致谢
【问题讨论】:
标签: php regex validation url-rewriting url-routing