$currentDir = "";
}
- foreach ($this->routes->all() as $routes) {
+ $allRoutes = $this->routes->all();
+
+ // reverse search, last registered route will overwrite
+ // previously registered route
+ for ($i = count($allRoutes) - 1; $i >= 0; $i--) {
+ $routes = $allRoutes[$i];
+
// compare server request method with route's allowed http methods
if (!in_array($requestMethod, (array)$routes->getMethods(), true)) {
continue;
);
}
+ public function testRouteOverwrite()
+ {
+ $collection = new RouteCollection();
+
+ $collection->attachRoute(new Route('/test', array(
+ '_controller' => '\PHPRouter\Test\Fixtures\CustomController::index',
+ 'methods' => 'GET',
+ 'name' => 'test_page',
+ )));
+ $collection->attachRoute(new Route('/test', array(
+ '_controller' => '\PHPRouter\Test\Fixtures\CustomController::_404',
+ 'methods' => 'GET',
+ 'name' => '404',
+ )));
+
+ $router = new Router($collection);
+
+ $_SERVER["REQUEST_URI"] = "/test";
+ $_SERVER["REQUEST_METHOD"] = "GET";
+
+ $hasRoute = $router->requestHasValidRoute();
+ $route = $router->getRequestRoute();
+
+ $this->assertTrue($hasRoute);
+ $this->assertEquals("404", $route->getName());
+
+ unset($_SERVER["REQUEST_URI"]);
+ unset($_SERVER["REQUEST_METHOD"]);
+ }
+
public function testParseConfig()
{
$config = Config::loadFromFile(__DIR__ . '/../../Fixtures/router.yaml');