mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
ENHANCEMENT: Added ErrorPage::response_for() to get a response for a HTTP error code and request.
MINOR: Updated ModelAsController to generate errors with ErrorPage. MINOR: Updated ContentController to generate HTTP errors with ErrorPage. From: Andrew Short <andrewjshort@gmail.com> git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@88480 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
3eafee7799
commit
acc063984b
@ -155,6 +155,14 @@ class ContentController extends Controller {
|
||||
return new $controllerClass($widget);
|
||||
}
|
||||
|
||||
/**
|
||||
* @uses ErrorPage::response_for()
|
||||
* @return HTTPResponse
|
||||
*/
|
||||
public function httpError($code, $message = null) {
|
||||
return ($resp = ErrorPage::response_for($code, $this->request)) ? $resp : parent::httpError($code, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the project name
|
||||
*
|
||||
|
@ -29,6 +29,8 @@ class ModelAsController extends Controller implements NestedController {
|
||||
|
||||
public function handleRequest($request) {
|
||||
$this->pushCurrent();
|
||||
|
||||
$this->request = $request;
|
||||
$this->urlParams = $request->allParams();
|
||||
|
||||
$this->init();
|
||||
@ -74,7 +76,7 @@ class ModelAsController extends Controller implements NestedController {
|
||||
return $response;
|
||||
}
|
||||
|
||||
$child = $this->get404Page();
|
||||
return ErrorPage::response_for(404, $this->request);
|
||||
}
|
||||
|
||||
if($child) {
|
||||
@ -137,15 +139,6 @@ class ModelAsController extends Controller implements NestedController {
|
||||
return false;
|
||||
}
|
||||
|
||||
protected function get404Page() {
|
||||
$page = DataObject::get_one("ErrorPage", "\"ErrorCode\" = '404'");
|
||||
if($page) {
|
||||
return $page;
|
||||
} else {
|
||||
// @deprecated 2.5 Use ErrorPage class
|
||||
return DataObject::get_one("SiteTree", "\"URLSegment\" = '404'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
@ -24,6 +24,46 @@ class ErrorPage extends Page {
|
||||
|
||||
protected static $static_filepath = ASSETS_PATH;
|
||||
|
||||
/**
|
||||
* Get a {@link HTTPResponse} that is suitable for responding to a reques that has thrown a specific error code.
|
||||
*
|
||||
* @param int $statusCode The HTTP error code.
|
||||
* @param HTTPRequest $request The request object that triggered the error.
|
||||
* @return HTTPResponse
|
||||
*/
|
||||
public static function response_for($statusCode, HTTPRequest $request = null) {
|
||||
$errorPage = null;
|
||||
|
||||
// First attempt to dynamically generate the error page, then fall back on using a cached version.
|
||||
if (
|
||||
(!$request || !$request->isMedia()) && !$errorPage = DataObject::get_one('ErrorPage', "\"ErrorCode\" = $statusCode")
|
||||
) {
|
||||
$cachedPath = self::get_filepath_for_errorcode($statusCode, Translatable::get_current_locale());
|
||||
|
||||
if(file_exists($cachedPath)) {
|
||||
$response = new HTTPResponse();
|
||||
|
||||
$response->setStatusCode($statusCode);
|
||||
$response->setBody(file_get_contents($cachedPath));
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
|
||||
// If the request is for a simple media type, or there was no matching dynamic or cached error page just return
|
||||
// a simple error string to avoid clogging up server resources.
|
||||
if(!$errorPage) {
|
||||
$response = new HTTPResponse();
|
||||
|
||||
$response->setStatusCode($statusCode);
|
||||
$response->setBody(sprintf('Error %s: %s', $response->getStatusCode(), $response->getStatusDescription()));
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
return ModelAsController::controller_for($errorPage)->handleRequest(new HTTPRequest('GET', ''));
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures that there is always a 404 page
|
||||
* by checking if there's an instance of
|
||||
|
Loading…
Reference in New Issue
Block a user