From 58e885e35f02735f25bccb0d3c810b0e3a6f4bbc Mon Sep 17 00:00:00 2001 From: Abel Hoogeveen Date: Tue, 14 Feb 2023 14:55:46 +0100 Subject: [PATCH] Added the setLocation() method for Output. Useful for redirecting the user to a different page and automatically setting the correct status header. --- src/FuzeWorks/Output.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/FuzeWorks/Output.php b/src/FuzeWorks/Output.php index fbca4ea..ca50b7c 100644 --- a/src/FuzeWorks/Output.php +++ b/src/FuzeWorks/Output.php @@ -522,4 +522,25 @@ class Output } } + + /** + * Set the location to redirect the user to. + * + * @param string $locationUrl Should be prepended with / + * @param bool $permanent True for 301, false for 302 redirect. + * @return void + */ + public function setLocation(string $locationUrl, bool $permanent = false) + { + // Set the status header + if ($permanent) + $this->setStatusHeader(301); + else + $this->setStatusHeader(302); + + // And the location itself + $header = 'Location: ' . $locationUrl; + $this->headers[] = [$header, true]; + } + } \ No newline at end of file