首先,添加一个简单的路由:
# The order is as-is in my environment
Route::PATTERN('/^\/style\/(.*?)\/$/', 'style/[0].css');
Route::PATTERN('/^\/style\/(.*?)\/image-storage\/(.*?)\/$/', 'transformed/images/[1]');
Route::SIMPLE('/blog', 'parts/blog');路由暂时不按优先级排序(这是一个选项,如果没有其他事情可以做,但我想避免它),活动路由被确定,例程在扫描后执行(foreach循环通过上面添加的路由,顺序相同):
foreach(self::$routes as $route){
// lookup active route
switch($route->type){
case self::TYPE_SIMPLE:
if($route->lookup === $_SERVER['REQUEST_URI']) self::$active = $route;
break;
case self::TYPE_PATTERN:
if(preg_match_all($route->lookup, $_SERVER['REQUEST_URI'], $found)){
// find all replaceable entries of route
preg_match_all('/((?<=\[)\d(?=\]))/', $route->location, $replace);
// remove first results
$lookup = array_shift($found);
$location = array_shift($replace);
// make the actual location out of both
foreach($replace[0] as $value)
if(isset($found[$value]))
$route->location = str_replace('[' . $value . ']', $found[$value][0], $route->location);
// this is active, cache it
self::$active = $route;
}
break;
}
}
// I have left out the active route and routine parsing, because the problem is here at the
// case self::TYPE_PATTERN part这里的问题是,第二个模式路由与第一个模式路由匹配。我可以通过先行查找来防止这种情况,但是路由稍后将变得动态和嵌套,因此不可预测的查找头可能/将会失败。
我有一个关于请求和查找(模式)的长度比较的想法,但这也是非常不可预测的。
那么,问题是,我如何才能避免这种碰撞?
发布于 2011-12-22 00:25:51
将不太通用的路线移到顶部,在另一条路线之上。然后,只有当它不匹配时,才会尝试更通用的方法:
Route::PATTERN('/^\/style\/(.*?)\/image-storage\/(.*?)\/$/', 'transformed/images/[1]');
Route::PATTERN('/^\/style\/(.*?)\/$/', 'style/[0].css');https://stackoverflow.com/questions/8592543
复制相似问题