From 71cd40b52ead2b6bc9f9f8489e0f70cef63c81cd Mon Sep 17 00:00:00 2001 From: gustavfors <49721755+gustavfors@users.noreply.github.com> Date: Tue, 5 Jan 2021 19:00:01 +0100 Subject: [PATCH] Added redirectBack method to the Response class Useful for redirecting the user back to the previous page. --- src/Response/Response.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/Response/Response.php b/src/Response/Response.php index 3ffc860..ea94a84 100644 --- a/src/Response/Response.php +++ b/src/Response/Response.php @@ -286,4 +286,22 @@ public function redirect(string $url) : object $this->body = null; return $this; } + + /** + * + * Tries to redirect back to the previous page + * + * @param string $fallback fallback url to redirect to + * + * @return self + * + */ + public function redirectBack(string $fallback = "") + { + if (isset($_SERVER["HTTP_REFERER"])) { + return $this->redirect($_SERVER["HTTP_REFERER"]); + } else { + return $this->redirect($fallback); + } + } }