From b14525aca96d9a4faa20c6281939fb9abb06949c Mon Sep 17 00:00:00 2001 From: Ostap Brehin Date: Sat, 5 Sep 2026 04:09:20 +0100 Subject: [PATCH 1/2] fix(router): clear the matched route before matching --- packages/router/src/MatchRouteMiddleware.php | 4 ++++ .../Router/MatchRouteMiddlewareTest.php | 24 +++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 tests/Integration/Router/MatchRouteMiddlewareTest.php diff --git a/packages/router/src/MatchRouteMiddleware.php b/packages/router/src/MatchRouteMiddleware.php index 87050102c..50d6cd81f 100644 --- a/packages/router/src/MatchRouteMiddleware.php +++ b/packages/router/src/MatchRouteMiddleware.php @@ -21,6 +21,10 @@ public function __construct( public function __invoke(Request $request, HttpMiddlewareCallable $next): Response { + // In long-running contexts, a previous request may have left a matched route behind. + // It must be cleared before matching, since we only rebind it on a successful match. + $this->container->unregister(MatchedRoute::class); + $matchedRoute = $this->routeMatcher->match($request); if (! $matchedRoute instanceof MatchedRoute && $request->method === Method::HEAD && $request instanceof GenericRequest) { diff --git a/tests/Integration/Router/MatchRouteMiddlewareTest.php b/tests/Integration/Router/MatchRouteMiddlewareTest.php new file mode 100644 index 000000000..e64883e33 --- /dev/null +++ b/tests/Integration/Router/MatchRouteMiddlewareTest.php @@ -0,0 +1,24 @@ +http->get('/repeated/a')->assertOk(); + + $this->assertTrue($this->container->has(MatchedRoute::class)); + + $this->http->get('/does-not-exist')->assertNotFound(); + + $this->assertFalse($this->container->has(MatchedRoute::class)); + } +} From c31738dde3f25708ca52aae0a6de4d0b83374f90 Mon Sep 17 00:00:00 2001 From: Ostap Brehin Date: Sat, 5 Sep 2026 04:10:05 +0100 Subject: [PATCH 2/2] fix(router): clear the matched route on container reset --- packages/router/src/RouterReset.php | 18 +++++++++++++++ tests/Integration/Router/RouterResetTest.php | 24 ++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 packages/router/src/RouterReset.php create mode 100644 tests/Integration/Router/RouterResetTest.php diff --git a/packages/router/src/RouterReset.php b/packages/router/src/RouterReset.php new file mode 100644 index 000000000..1b40c4218 --- /dev/null +++ b/packages/router/src/RouterReset.php @@ -0,0 +1,18 @@ +container->unregister(MatchedRoute::class); + } +} diff --git a/tests/Integration/Router/RouterResetTest.php b/tests/Integration/Router/RouterResetTest.php new file mode 100644 index 000000000..66c26ad86 --- /dev/null +++ b/tests/Integration/Router/RouterResetTest.php @@ -0,0 +1,24 @@ +http->get('/repeated/a')->assertOk(); + + $this->assertTrue($this->container->has(MatchedRoute::class)); + + $this->container->reset(); + + $this->assertFalse($this->container->has(MatchedRoute::class)); + } +}