Skip to content

Commit 21793e3

Browse files
committed
(fix): reset allowOverride in Router::reset() and guard duplicate alias entries
1 parent 7618804 commit 21793e3

3 files changed

Lines changed: 9 additions & 2 deletions

File tree

src/Http/Route.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ public function alias(string $path): self
8989
Router::addRouteAlias($path, $this, $method);
9090
}
9191

92-
$this->aliasPaths[] = $path;
92+
if (!\in_array($path, $this->aliasPaths, true)) {
93+
$this->aliasPaths[] = $path;
94+
}
9395

9496
return $this;
9597
}
@@ -108,7 +110,9 @@ public function aliasMethod(string $method): self
108110
Router::addRouteAlias($path, $this, $method);
109111
}
110112

111-
$this->aliasMethods[] = $method;
113+
if (!\in_array($method, $this->aliasMethods, true)) {
114+
$this->aliasMethods[] = $method;
115+
}
112116

113117
return $this;
114118
}

src/Http/Router.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ public static function reset(): void
266266
{
267267
self::$params = [];
268268
self::$wildcard = null;
269+
self::$allowOverride = false;
269270
self::$routes = [
270271
Http::REQUEST_METHOD_GET => [],
271272
Http::REQUEST_METHOD_POST => [],

tests/RouterTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,10 @@ public function testCanOverrideMethodAlias(): void
215215
$routeGET = new Route(Http::REQUEST_METHOD_GET, '/userinfo');
216216
Router::addRoute($routeGET);
217217
$routeGET->aliasMethod(Http::REQUEST_METHOD_POST);
218+
$routeGET->aliasMethod(Http::REQUEST_METHOD_POST);
218219

219220
$this->assertEquals($routeGET, Router::match(Http::REQUEST_METHOD_POST, '/userinfo')?->route);
221+
$this->assertSame([Http::REQUEST_METHOD_POST], $routeGET->getAliasMethods());
220222
} finally {
221223
Router::setAllowOverride(false);
222224
}

0 commit comments

Comments
 (0)