From c0660c147bf1e76a9c4864e0dae3d45cd5376867 Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Fri, 31 Aug 2007 00:26:41 +0000 Subject: [PATCH] Added Director::redirected_to / Controller::redirectedTo() for redirection-detection git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@41093 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/control/Controller.php | 8 ++++++++ core/control/Director.php | 14 +++++++++++--- core/control/HTTPResponse.php | 6 +++++- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/core/control/Controller.php b/core/control/Controller.php index 20ab8cfce..37fe78859 100644 --- a/core/control/Controller.php +++ b/core/control/Controller.php @@ -414,6 +414,14 @@ class Controller extends ViewableData { $this->response->redirect($url); } + /** + * Tests whether a redirection has been requested. + * @return string If redirect() has been called, it will return the URL redirected to. Otherwise, it will return null; + */ + function redirectedTo() { + return $this->response->getHeader('Location'); + } + /** * Get the Session object representing this Controller's session * @return Session diff --git a/core/control/Director.php b/core/control/Director.php index 8f387b9b6..7cdfe1eef 100644 --- a/core/control/Director.php +++ b/core/control/Director.php @@ -188,14 +188,22 @@ class Director { /** * Redirect to another page. - * - $url can be an absolute URL - * - or it can be a URL relative to the "site base" - * - if it is just a word without an slashes, then it redirects to another action on the current controller. + * - $url can be an absolute URL + * - or it can be a URL relative to the "site base" + * - if it is just a word without an slashes, then it redirects to another action on the current controller. */ static function redirect($url) { Controller::curr()->redirect($url); } + /** + * Tests whether a redirection has been requested. + * @return string If redirect() has been called, it will return the URL redirected to. Otherwise, it will return null; + */ + static function redirected_to() { + Controller::curr()->redirectedTo(); + } + /** * Uses either the HTTP_REFERER or a manually set request-variable called _REDIRECT_BACK_URL. * This variable is needed in scenarios where not HTTP-Referer is sent (e.g when calling a page diff --git a/core/control/HTTPResponse.php b/core/control/HTTPResponse.php index 76e32f44d..99a5dd995 100644 --- a/core/control/HTTPResponse.php +++ b/core/control/HTTPResponse.php @@ -73,7 +73,11 @@ class HTTPResponse extends Object { * @returns string */ function getHeader($header) { - return $this->headers[$header]; + if(isset($this->headers[$header])) { + return $this->headers[$header]; + } else { + return null; + } } function redirect($dest) {