FEATURE: Allow you to access nested pages by falling over to a child page in ContentController if one is available.

From: Andrew Short <andrewjshort@gmail.com>

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@88483 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Andrew Short 2009-10-11 00:07:07 +00:00 committed by Sam Minnee
parent 264b484e82
commit da2fd13b38
4 changed files with 125 additions and 0 deletions

View File

@ -155,6 +155,32 @@ class ContentController extends Controller {
return new $controllerClass($widget); return new $controllerClass($widget);
} }
/**
* 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 HTTPResponse
*/
public function handleRequest(HTTPRequest $request) {
$response = parent::handleRequest($request);
// If the default handler returns an error, due to the action not existing, attempt to fall over to a child
// SiteTree object, then use its corresponding ContentController to handle the request. This allows for the
// building of nested chains of controllers corresponding to a nested URL.
if(SiteTree::nested_urls() && $response instanceof HTTPResponse && $response->isError()) {
$SQL_URLParam = Convert::raw2sql($request->param('Action'));
if($SQL_URLParam && $nextPage = DataObject::get_one('SiteTree', "\"ParentID\" = $this->ID AND \"URLSegment\" = '$SQL_URLParam'")) {
if($nextPage->canView()) {
$request->shiftAllParams();
return ModelAsController::controller_for($nextPage)->handleRequest($request);
}
}
}
return $response;
}
/** /**
* @uses ErrorPage::response_for() * @uses ErrorPage::response_for()
* @return HTTPResponse * @return HTTPResponse

18
core/control/HTTPRequest.php Normal file → Executable file
View File

@ -381,6 +381,24 @@ class HTTPRequest extends Object implements ArrayAccess {
function allParams() { function allParams() {
return $this->allParams; return $this->allParams;
} }
/**
* Shift all the parameter values down a key space, and return the shifted value.
*
* @return string
*/
public function shiftAllParams() {
$keys = array_keys($this->allParams);
$values = array_values($this->allParams);
$value = array_shift($values);
foreach($keys as $position => $key) {
$this->allParams[$key] = isset($values[$position]) ? $values[$position] : null;
}
return $value;
}
function latestParams() { function latestParams() {
return $this->latestParams; return $this->latestParams;
} }

View File

@ -0,0 +1,65 @@
<?php
/**
* @package sapphire
* @subpackage tests
*/
class ContentControllerTest extends FunctionalTest {
public static $fixture_file = 'sapphire/tests/control/ContentControllerTest.yml';
public static $use_draft_site = true;
/**
* Test that nested pages, basic actions, and nested/non-nested URL switching works properly
*/
public function testNestedPages() {
SiteTree::enable_nested_urls();
$this->assertEquals('Home Page', $this->get('/')->getBody());
$this->assertEquals('Home Page', $this->get('/home/index/')->getBody());
$this->assertEquals('Home Page', $this->get('/home/second-index/')->getBody());
$this->assertEquals('Second Level Page', $this->get('/home/second-level/')->getBody());
$this->assertEquals('Second Level Page', $this->get('/home/second-level/index/')->getBody());
$this->assertEquals('Second Level Page', $this->get('/home/second-level/second-index/')->getBody());
$this->assertEquals('Third Level Page', $this->get('/home/second-level/third-level/')->getBody());
$this->assertEquals('Third Level Page', $this->get('/home/second-level/third-level/index/')->getBody());
$this->assertEquals('Third Level Page', $this->get('/home/second-level/third-level/second-index/')->getBody());
SiteTree::disable_nested_urls();
$this->assertEquals('Home Page', $this->get('/')->getBody());
$this->assertEquals('Home Page', $this->get('/home/')->getBody());
$this->assertEquals('Home Page', $this->get('/home/second-index/')->getBody());
$this->assertEquals('Second Level Page', $this->get('/second-level/')->getBody());
$this->assertEquals('Second Level Page', $this->get('/second-level/index/')->getBody());
$this->assertEquals('Second Level Page', $this->get('/second-level/second-index/')->getBody());
$this->assertEquals('Third Level Page', $this->get('/third-level/')->getBody());
$this->assertEquals('Third Level Page', $this->get('/third-level/index/')->getBody());
$this->assertEquals('Third Level Page', $this->get('/third-level/second-index/')->getBody());
}
}
class ContentControllerTest_Page extends Page {
public static $allowed_actions = array (
'second_index'
);
}
class ContentControllerTest_Page_Controller extends Page_Controller {
public function index() {
return $this->Title;
}
public function second_index() {
return $this->index();
}
}

View File

@ -0,0 +1,16 @@
ContentControllerTest_Page:
root_page:
Title: Home Page
URLSegment: home
second_level_page:
Title: Second Level Page
URLSegment: second-level
Parent: =>ContentControllerTest_Page.root_page
third_level_page:
Title: Third Level Page
URLSegment: third-level
Parent: =>ContentControllerTest_Page.second_level_page
third_level_page_2:
Title: Third Level Page Two
URLSegment: third-level-2
Parent: =>ContentControllerTest_Page.second_level_page