From e19ec7d93f0729b46986b731196c010601b55537 Mon Sep 17 00:00:00 2001 From: Andrew O'Neil Date: Sun, 2 Dec 2007 21:29:31 +0000 Subject: [PATCH] #1432, #1557 - Added Director::set_status_code() and Director::get_status_code(), and used this to fix 404 errors. (merged from branches/2.2.0@45907, r45443) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@46099 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/control/Controller.php | 8 ++++++++ core/control/Director.php | 14 ++++++++++++++ core/control/HTTPResponse.php | 3 +++ core/control/ModelAsController.php | 3 +-- core/model/ErrorPage.php | 5 ----- 5 files changed, 26 insertions(+), 7 deletions(-) diff --git a/core/control/Controller.php b/core/control/Controller.php index 293c14fc6..3f6f8c787 100644 --- a/core/control/Controller.php +++ b/core/control/Controller.php @@ -44,6 +44,14 @@ class Controller extends ViewableData { function getURLParams() { return $this->urlParams; } + + /** + * Returns the HTTPResponse object that this controller is building up. + * Can be used to set the status code and headers + */ + function getResponse() { + return $this->response; + } /** * Execute the appropriate action handler. If none is given, use defaultAction to display diff --git a/core/control/Director.php b/core/control/Director.php index 1ab301794..512a1bd28 100644 --- a/core/control/Director.php +++ b/core/control/Director.php @@ -242,6 +242,20 @@ class Director { static function redirected_to() { return Controller::curr()->redirectedTo(); } + + /** + * Sets the HTTP status code + */ + static function set_status_code($code) { + return Controller::curr()->getResponse()->setStatusCode($code); + } + + /** + * Returns the current HTTP status code + */ + static function get_status_code() { + return Controller::curr()->getResponse()->getStatusCode(); + } /* * Redirect back diff --git a/core/control/HTTPResponse.php b/core/control/HTTPResponse.php index 99a5dd995..b667aeba1 100644 --- a/core/control/HTTPResponse.php +++ b/core/control/HTTPResponse.php @@ -53,6 +53,9 @@ class HTTPResponse extends Object { if(isset(self::$status_codes[$code])) $this->statusCode = $code; else user_error("Unrecognised HTTP status code '$code'", E_USER_WARNING); } + function getStatusCode() { + return $this->statusCode; + } function setBody($body) { $this->body = $body; diff --git a/core/control/ModelAsController.php b/core/control/ModelAsController.php index e7a047e94..ebf88d439 100644 --- a/core/control/ModelAsController.php +++ b/core/control/ModelAsController.php @@ -31,8 +31,7 @@ class ModelAsController extends Controller implements NestedController { $child = DataObject::get_one("SiteTree", "URLSegment = '$SQL_URLSegment'"); } if(!$child) { - - header("HTTP/1.0 404 Not Found"); + Director::set_status_code(404); $child = $this->get404Page(); } diff --git a/core/model/ErrorPage.php b/core/model/ErrorPage.php index 6972798e6..c67752196 100755 --- a/core/model/ErrorPage.php +++ b/core/model/ErrorPage.php @@ -106,11 +106,6 @@ class ErrorPage extends Page { * Controller for ErrorPages. */ class ErrorPage_Controller extends Page_Controller { - function init() { - parent::init(); - - $this->response->setStatusCode($this->dataRecord->ErrorCode); - } }