mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
#2679: Auto-redirect renamed pages
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@65781 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
2f25dda9de
commit
f11d7cc960
@ -13,11 +13,10 @@ class ModelAsController extends Controller implements NestedController {
|
||||
$this->urlParams = $request->allParams();
|
||||
|
||||
$this->init();
|
||||
$nested = $this->getNestedController();
|
||||
if(is_object($nested)) {
|
||||
$result = $nested->handleRequest($request);
|
||||
} else {
|
||||
$result = $nested;
|
||||
$result = $this->getNestedController();
|
||||
|
||||
if(is_object($result) && $result instanceOf RequestHandler) {
|
||||
$result = $result->handleRequest($request);
|
||||
}
|
||||
|
||||
$this->popCurrent();
|
||||
@ -39,6 +38,20 @@ class ModelAsController extends Controller implements NestedController {
|
||||
$child = DataObject::get_one("SiteTree", "URLSegment = '$SQL_URLSegment'", false);
|
||||
}
|
||||
if(!$child) {
|
||||
if($child = $this->findOldPage($SQL_URLSegment)) {
|
||||
$url = Controller::join_links(
|
||||
Director::baseURL(),
|
||||
$child->URLSegment,
|
||||
$this->urlParams['Action'],
|
||||
$this->urlParams['ID'],
|
||||
$this->urlParams['OtherID']
|
||||
);
|
||||
|
||||
$response = new HTTPResponse();
|
||||
$response->redirect($url, 301);
|
||||
return $response;
|
||||
}
|
||||
|
||||
$child = $this->get404Page();
|
||||
}
|
||||
|
||||
@ -67,6 +80,23 @@ class ModelAsController extends Controller implements NestedController {
|
||||
}
|
||||
}
|
||||
|
||||
protected function findOldPage($urlSegment) {
|
||||
$versionedQuery = new SQLQuery (
|
||||
'RecordID', 'SiteTree_versions',
|
||||
"`WasPublished` = 1 AND `URLSegment` = '$urlSegment'",
|
||||
'`LastEdited` DESC, `WasPublished`',
|
||||
null, null, 1
|
||||
);
|
||||
|
||||
$result = $versionedQuery->execute();
|
||||
|
||||
if($result->numRecords() == 1 && $redirectPage = $result->nextRecord()) {
|
||||
if($redirectObj = DataObject::get_by_id('SiteTree', $redirectPage['RecordID'])) return $redirectObj;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected function get404Page() {
|
||||
if($page = DataObject::get_one("ErrorPage", "ErrorCode = '404'")) return $page;
|
||||
else return DataObject::get_one("SiteTree", "URLSegment = '404'");
|
||||
|
Loading…
x
Reference in New Issue
Block a user