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