Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions flight/net/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public function map(string $pattern, $callback, bool $pass_route = false, string
if (!isset($this->routesByMethod[$method])) {
$this->routesByMethod[$method] = [];
}
$this->routesByMethod[$method][] = $route;
$this->routesByMethod[$method][count($this->routes) - 1] = $route;
}

return $route;
Expand Down Expand Up @@ -270,17 +270,13 @@ public function route(Request $request)
}

// 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;
}
}

Expand Down
17 changes: 17 additions & 0 deletions tests/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -780,4 +780,21 @@ public function testStripMultipleSlashesFromUrlAndStillMatch(): void
$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