Skip to content

Commit 15cfdb0

Browse files
authored
Merge pull request #694 from KnifeLemon/master
fix wildcard routes being skipped by fast path method lookup closes #693
2 parents fc43882 + 02fe881 commit 15cfdb0

2 files changed

Lines changed: 25 additions & 12 deletions

File tree

flight/net/Router.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public function map(string $pattern, $callback, bool $pass_route = false, string
152152
if (!isset($this->routesByMethod[$method])) {
153153
$this->routesByMethod[$method] = [];
154154
}
155-
$this->routesByMethod[$method][] = $route;
155+
$this->routesByMethod[$method][count($this->routes) - 1] = $route;
156156
}
157157

158158
return $route;
@@ -270,17 +270,13 @@ public function route(Request $request)
270270
}
271271

272272
// Fast path: check method-specific routes first, then wildcard routes (only on first routing attempt)
273-
$methodsToCheck = [$requestMethod, '*'];
274-
foreach ($methodsToCheck as $method) {
275-
if (isset($this->routesByMethod[$method])) {
276-
foreach ($this->routesByMethod[$method] as $route) {
277-
if ($route->matchUrl($requestUrl, $this->caseSensitive)) {
278-
$this->executedRoute = $route;
279-
// Set iterator position to this route for potential next() calls
280-
$this->index = array_search($route, $this->routes, true);
281-
return $route;
282-
}
283-
}
273+
$candidates = ($this->routesByMethod[$requestMethod] ?? []) + ($this->routesByMethod['*'] ?? []);
274+
ksort($candidates);
275+
foreach ($candidates as $routeIndex => $route) {
276+
if ($route->matchUrl($requestUrl, $this->caseSensitive)) {
277+
$this->executedRoute = $route;
278+
$this->index = $routeIndex;
279+
return $route;
284280
}
285281
}
286282

tests/RouterTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,4 +780,21 @@ public function testStripMultipleSlashesFromUrlAndStillMatch(): void
780780
$this->request->method = 'GET';
781781
$this->check('OK');
782782
}
783+
784+
public function testWildcardPassthroughRouteBeforeSpecificGetRoute(): void
785+
{
786+
$this->router->map('/@par/[^\/]+/*', function (string $par): bool {
787+
echo "Passthrough (Par = $par)";
788+
return true;
789+
});
790+
791+
$this->router->map('GET /[^\/]+/target', function (): void {
792+
echo ' Target';
793+
});
794+
795+
$this->request->url = '/foobar/target/';
796+
$this->request->method = 'GET';
797+
798+
$this->check('Passthrough (Par = foobar) Target');
799+
}
783800
}

0 commit comments

Comments
 (0)