Add redirect response builder to controller.

This commit is contained in:
Madeorsk 2024-11-09 10:04:54 +01:00
parent 9964d87e1e
commit 46f0ad4ed5
Signed by: Madeorsk
GPG key ID: 677E51CA765BB79F

View file

@ -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. * Build a text response.
* @param string $value The text value to pass. * @param string $value The text value to pass.