From eed3b4f80533fa9fe54b35a5158c47369915fe0e Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 18 Nov 2023 17:28:40 +0100 Subject: [PATCH] Allow specifying status code in `View::redirect()` --- src/main/php/web/frontend/View.class.php | 5 +++-- src/test/php/web/frontend/unittest/ViewTest.class.php | 8 ++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/main/php/web/frontend/View.class.php b/src/main/php/web/frontend/View.class.php index 17718a9..b24e3de 100755 --- a/src/main/php/web/frontend/View.class.php +++ b/src/main/php/web/frontend/View.class.php @@ -31,11 +31,12 @@ public static function named($template) { * Redirects to another URL * * @param string|util.URI $uri + * @param int $status Defaults to 302 * @return self */ - public static function redirect($url) { + public static function redirect($url, $status= 302) { $self= new self(null); - $self->status= 302; + $self->status= $status; $self->headers['Location']= $url; $self->headers['Content-Length']= 0; $self->stream= false; diff --git a/src/test/php/web/frontend/unittest/ViewTest.class.php b/src/test/php/web/frontend/unittest/ViewTest.class.php index eeebda9..660330a 100755 --- a/src/test/php/web/frontend/unittest/ViewTest.class.php +++ b/src/test/php/web/frontend/unittest/ViewTest.class.php @@ -54,6 +54,14 @@ public function redirect_sets_location_and_status() { Assert::equals('http://test', $redirect->headers['Location']); } + #[Test] + public function redirect_using_307() { + $redirect= View::redirect('http://test', 307); + + Assert::equals(307, $redirect->status); + Assert::equals('http://test', $redirect->headers['Location']); + } + #[Test] public function error_sets_template_and_status() { $redirect= View::error(404);