路由,如果请求匹配多于1条路由?

时间:2017-11-12 04:00:59

标签: php api routing

我在这里使用代码(https://github.com/gregdel/php-router)作为重建我的简单路由器作为RESTful API的基础。

虽然在router.php的第49-52行(参见代码片段)中,开发人员使用循环,这意味着单个请求(方法和模式)有多个匹配。

我的问题是,是否应该为方法/模式进行多次匹配?我的理解是永远不会发生这种情况。

若在何种情况下如此?

//Run the matching routes
foreach ($this->_matchingRoutes as $route) {
    $route->run();
}

1 个答案:

答案 0 :(得分:1)

看看这个功能:



private function _findMatchingPattern($routes, $URI) {
    //Reset the matching pattern array
	$this->_matchingRoutes = array();
    foreach ($routes as $route) {
		if ($route->patternMatches($URI))
			array_push($this->_matchingRoutes, $route);
	}
}



 $ routes数组可能包含多个路由,而不是一个。 然后,可以有几个匹配。

相关问题