/** * This acts the same as {@link Controller::handleRequest()}, but if an action cannot be found this will attempt to * fall over to a child controller in order to provide functionality for nested URLs. * * @return SS_HTTPResponse */ public function handleRequest(SS_HTTPRequest $request) { $child = null; $action = $request->param('Action'); // If nested URLs are enabled, and there is no action handler for the current request then attempt to pass // control to a child controller. This allows for the creation of chains of controllers which correspond to a // nested URL. if ($action && SiteTree::nested_urls() && !$this->hasAction($action)) { // See ModelAdController->getNestedController() for similar logic Translatable::disable_locale_filter(); // look for a page with this URLSegment $child = DataObject::get_one('SiteTree', sprintf("\"ParentID\" = %s AND \"URLSegment\" = '%s'", $this->ID, Convert::raw2sql($action))); Translatable::enable_locale_filter(); // if we can't find a page with this URLSegment try to find one that used to have // that URLSegment but changed. See ModelAsController->getNestedController() for similiar logic. if (!$child) { $child = ModelAsController::find_old_page($action, $this->ID); if ($child) { $response = new SS_HTTPResponse(); $params = $request->getVars(); if (isset($params['url'])) { unset($params['url']); } $response->redirect(Controller::join_links($child->Link(Controller::join_links($request->param('ID'), $request->param('OtherID'))), $params ? '?' . http_build_query($params) : null), 301); return $response; } } } // we found a page with this URLSegment. if ($child) { $request->shiftAllParams(); $request->shift(); $response = ModelAsController::controller_for($child)->handleRequest($request); } else { // If a specific locale is requested, and it doesn't match the page found by URLSegment, // look for a translation and redirect (see #5001). Only happens on the last child in // a potentially nested URL chain. if ($request->getVar('locale') && $this->dataRecord && $this->dataRecord->Locale != $request->getVar('locale')) { $translation = $this->dataRecord->getTranslation($request->getVar('locale')); if ($translation) { $response = new SS_HTTPResponse(); $response->redirect($translation->Link(), 301); throw new SS_HTTPResponse_Exception($response); } } Director::set_current_page($this->data()); $response = parent::handleRequest($request); Director::set_current_page(null); } return $response; }
/** * * NOTE: This test requires nested_urls * */ function testFindOldPage() { $page = new Page(); $page->Title = 'First Level'; $page->URLSegment = 'oldurl'; $page->write(); $page->publish('Stage', 'Live'); $page->URLSegment = 'newurl'; $page->write(); $page->publish('Stage', 'Live'); $response = ModelAsController::find_old_page('oldurl'); $this->assertEquals('First Level', $response->Title); $page2 = new Page(); $page2->Title = 'Second Level Page'; $page2->URLSegment = 'oldpage2'; $page2->ParentID = $page->ID; $page2->write(); $page2->publish('Stage', 'Live'); $page2->URLSegment = 'newpage2'; $page2->write(); $page2->publish('Stage', 'Live'); $response = ModelAsController::find_old_page('oldpage2', $page2->ParentID); $this->assertEquals('Second Level Page', $response->Title); $response = ModelAsController::find_old_page('oldpage2', $page2->ID); $this->assertEquals(false, $response); }