diff --git a/system/HTTP/RedirectResponse.php b/system/HTTP/RedirectResponse.php index b9cabbba6793..1040c1578628 100644 --- a/system/HTTP/RedirectResponse.php +++ b/system/HTTP/RedirectResponse.php @@ -44,16 +44,20 @@ public function to(string $uri, ?int $code = null, string $method = 'auto') * Sets the URI to redirect to but as a reverse-routed or named route * instead of a raw URI. * + * @param string $route Named route or Controller::method + * * @throws HTTPException * * @return $this */ public function route(string $route, array $params = [], int $code = 302, string $method = 'auto') { + $namedRoute = $route; + $route = Services::routes()->reverseRoute($route, ...$params); if (! $route) { - throw HTTPException::forInvalidRedirectRoute($route); + throw HTTPException::forInvalidRedirectRoute($namedRoute); } return $this->redirect(site_url($route), $method, $code); diff --git a/system/Language/en/HTTP.php b/system/Language/en/HTTP.php index 8902b8ea4d89..5ee3cae27bc3 100644 --- a/system/Language/en/HTTP.php +++ b/system/Language/en/HTTP.php @@ -27,7 +27,7 @@ 'emptySupportedNegotiations' => 'You must provide an array of supported values to all Negotiations.', // RedirectResponse - 'invalidRoute' => '{0} route cannot be found while reverse-routing.', + 'invalidRoute' => 'The route for "{0}" cannot be found.', // DownloadResponse 'cannotSetBinary' => 'When setting filepath cannot set binary.', diff --git a/tests/system/HTTP/RedirectResponseTest.php b/tests/system/HTTP/RedirectResponseTest.php index 9773ab8c7b72..95cd6003f275 100644 --- a/tests/system/HTTP/RedirectResponseTest.php +++ b/tests/system/HTTP/RedirectResponseTest.php @@ -81,9 +81,10 @@ public function testRedirectRoute() $this->assertSame('http://example.com/index.php/exampleRoute', $response->getHeaderLine('Location')); } - public function testRedirectRouteBad() + public function testRedirectRouteBadNamedRoute() { $this->expectException(HTTPException::class); + $this->expectExceptionMessage('The route for "differentRoute" cannot be found.'); $response = new RedirectResponse(new App()); @@ -92,6 +93,18 @@ public function testRedirectRouteBad() $response->route('differentRoute'); } + public function testRedirectRouteBadControllerMethod() + { + $this->expectException(HTTPException::class); + $this->expectExceptionMessage('The route for "Bad::badMethod" cannot be found.'); + + $response = new RedirectResponse(new App()); + + $this->routes->add('exampleRoute', 'Home::index'); + + $response->route('Bad::badMethod'); + } + public function testRedirectRelativeConvertsToFullURI() { $response = new RedirectResponse($this->config);