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); - } }