Merge pull request #694 from KnifeLemon/master

fix wildcard routes being skipped by fast path method lookup closes #693
master
n0nag0n 3 days ago committed by GitHub
commit 15cfdb01df
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -152,7 +152,7 @@ class Router
if (!isset($this->routesByMethod[$method])) {
$this->routesByMethod[$method] = [];
}
$this->routesByMethod[$method][] = $route;
$this->routesByMethod[$method][count($this->routes) - 1] = $route;
}
return $route;
@ -270,17 +270,13 @@ class Router
}
// Fast path: check method-specific routes first, then wildcard routes (only on first routing attempt)
$methodsToCheck = [$requestMethod, '*'];
foreach ($methodsToCheck as $method) {
if (isset($this->routesByMethod[$method])) {
foreach ($this->routesByMethod[$method] as $route) {
if ($route->matchUrl($requestUrl, $this->caseSensitive)) {
$this->executedRoute = $route;
// Set iterator position to this route for potential next() calls
$this->index = array_search($route, $this->routes, true);
return $route;
}
}
$candidates = ($this->routesByMethod[$requestMethod] ?? []) + ($this->routesByMethod['*'] ?? []);
ksort($candidates);
foreach ($candidates as $routeIndex => $route) {
if ($route->matchUrl($requestUrl, $this->caseSensitive)) {
$this->executedRoute = $route;
$this->index = $routeIndex;
return $route;
}
}

@ -780,4 +780,21 @@ class RouterTest extends TestCase
$this->request->method = 'GET';
$this->check('OK');
}
public function testWildcardPassthroughRouteBeforeSpecificGetRoute(): void
{
$this->router->map('/@par/[^\/]+/*', function (string $par): bool {
echo "Passthrough (Par = $par)";
return true;
});
$this->router->map('GET /[^\/]+/target', function (): void {
echo ' Target';
});
$this->request->url = '/foobar/target/';
$this->request->method = 'GET';
$this->check('Passthrough (Par = foobar) Target');
}
}

Loading…
Cancel
Save