1
0
mirror of https://github.com/dannyvankooten/AltoRouter.git synced 2025-03-15 06:49:38 +01:00
This commit is contained in:
Danny van Kooten 2019-11-23 12:01:41 +01:00
parent a80bb36f11
commit 127f6e9699
2 changed files with 8 additions and 5 deletions

View File

@ -204,6 +204,8 @@ class AltoRouter
$requestUrl = substr($requestUrl, 0, $strpos);
}
$lastRequestUrlChar = $requestUrl[strlen($requestUrl)-1];
// set Request Method if it isn't passed as a parameter
if ($requestMethod === null) {
$requestMethod = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET';
@ -230,10 +232,12 @@ class AltoRouter
// No params in url, do string comparison
$match = strcmp($requestUrl, $route) === 0;
} else {
// Compare longest non-param string with url
if (strncmp($requestUrl, $route, $position) !== 0) {
// Compare longest non-param string with url before moving on to regex
// Check if last character before param is a slash, because it could be optional if param is optional too (see https://github.com/dannyvankooten/AltoRouter/issues/241)
if (strncmp($requestUrl, $route, $position) !== 0 && ($lastRequestUrlChar === '/' || $route[$position-1] !== '/')) {
continue;
}
$regex = $this->compileRoute($route);
$match = preg_match($regex, $requestUrl, $params) === 1;
}
@ -254,6 +258,7 @@ class AltoRouter
];
}
}
return false;
}

View File

@ -371,7 +371,7 @@ class AltoRouterTest extends PHPUnit\Framework\TestCase
->getMock();
// this should prove that compileRoute is not called when the route doesn't
// have any params in it, but this doesn't work because compileRoute is private.
// have any params in it
$router->expects($this->never())
->method('compileRoute');
@ -384,8 +384,6 @@ class AltoRouterTest extends PHPUnit\Framework\TestCase
'name' => 'contact'
], $router->match('/contact', 'GET'));
$router->map('GET', '/page/[:id]', 'pages#show', 'page');
// no prefix match, so no regex compilation necessary
$this->assertFalse($router->match('/page1', 'GET'));
}