From 46f0ad4ed57e1e617ed6470e8cde0381200c433f Mon Sep 17 00:00:00 2001 From: Madeorsk Date: Sat, 9 Nov 2024 10:04:54 +0100 Subject: [PATCH] Add redirect response builder to controller. --- src/Router/Controller.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/Router/Controller.php b/src/Router/Controller.php index a99c4b6..6062d85 100644 --- a/src/Router/Controller.php +++ b/src/Router/Controller.php @@ -57,6 +57,22 @@ abstract class Controller ); } + /** + * Build a redirect response. + * @param string $to Location to redirect to. + * @param StatusCode $statusCode The status code to send back. 301 Moved Permanently by default. + * @return ResponseInterface Built response. + */ + public function redirect(string $to, StatusCode $statusCode = StatusCode::MovedPermanently): ResponseInterface + { + return new Response( + // Pass the status code. + status: $statusCode->value, + // Pass redirect location. + headers: ["Location" => $to], + ); + } + /** * Build a text response. * @param string $value The text value to pass.