1
0
mirror of https://github.com/dannyvankooten/AltoRouter.git synced 2025-08-05 16:07:32 +02: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); $requestUrl = substr($requestUrl, 0, $strpos);
} }
$lastRequestUrlChar = $requestUrl[strlen($requestUrl)-1];
// set Request Method if it isn't passed as a parameter // set Request Method if it isn't passed as a parameter
if ($requestMethod === null) { if ($requestMethod === null) {
$requestMethod = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET'; $requestMethod = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET';
@@ -230,10 +232,12 @@ class AltoRouter
// No params in url, do string comparison // No params in url, do string comparison
$match = strcmp($requestUrl, $route) === 0; $match = strcmp($requestUrl, $route) === 0;
} else { } else {
// Compare longest non-param string with url // Compare longest non-param string with url before moving on to regex
if (strncmp($requestUrl, $route, $position) !== 0) { // 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; continue;
} }
$regex = $this->compileRoute($route); $regex = $this->compileRoute($route);
$match = preg_match($regex, $requestUrl, $params) === 1; $match = preg_match($regex, $requestUrl, $params) === 1;
} }
@@ -254,6 +258,7 @@ class AltoRouter
]; ];
} }
} }
return false; return false;
} }

View File

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