#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
This commit is contained in:
Andrew O'Neil 2007-12-02 21:29:31 +00:00
parent 4e5bdfc3fe
commit e19ec7d93f
5 changed files with 26 additions and 7 deletions

View File

@ -45,6 +45,14 @@ class Controller extends ViewableData {
return $this->urlParams; 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 * Execute the appropriate action handler. If none is given, use defaultAction to display
* a template. The default action will be appropriate in most cases where displaying data * a template. The default action will be appropriate in most cases where displaying data

View File

@ -243,6 +243,20 @@ class Director {
return Controller::curr()->redirectedTo(); 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 * Redirect back
* *

View File

@ -53,6 +53,9 @@ class HTTPResponse extends Object {
if(isset(self::$status_codes[$code])) $this->statusCode = $code; if(isset(self::$status_codes[$code])) $this->statusCode = $code;
else user_error("Unrecognised HTTP status code '$code'", E_USER_WARNING); else user_error("Unrecognised HTTP status code '$code'", E_USER_WARNING);
} }
function getStatusCode() {
return $this->statusCode;
}
function setBody($body) { function setBody($body) {
$this->body = $body; $this->body = $body;

View File

@ -31,8 +31,7 @@ class ModelAsController extends Controller implements NestedController {
$child = DataObject::get_one("SiteTree", "URLSegment = '$SQL_URLSegment'"); $child = DataObject::get_one("SiteTree", "URLSegment = '$SQL_URLSegment'");
} }
if(!$child) { if(!$child) {
Director::set_status_code(404);
header("HTTP/1.0 404 Not Found");
$child = $this->get404Page(); $child = $this->get404Page();
} }

View File

@ -106,11 +106,6 @@ class ErrorPage extends Page {
* Controller for ErrorPages. * Controller for ErrorPages.
*/ */
class ErrorPage_Controller extends Page_Controller { class ErrorPage_Controller extends Page_Controller {
function init() {
parent::init();
$this->response->setStatusCode($this->dataRecord->ErrorCode);
}
} }