Added the setLocation() method for Output.

Useful for redirecting the user to a different page and automatically setting the correct status header.
This commit is contained in:
Abel Hoogeveen 2023-02-14 14:55:46 +01:00
parent d6863d3f51
commit 58e885e35f
1 changed files with 21 additions and 0 deletions

View File

@ -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];
}
}