From b6589ba9a94f2ba532813cc15be1df58c37d2c61 Mon Sep 17 00:00:00 2001 From: Loz Calver Date: Wed, 23 Oct 2013 14:54:07 +0100 Subject: [PATCH] NEW: Page types no longer require a controller, they can inherit the parent page type's Use ClassInfo::ancestry() instead of get_parent_class() Fall back to ContentController by default --- code/controllers/ModelAsController.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/code/controllers/ModelAsController.php b/code/controllers/ModelAsController.php index 2563f35d..b4495fbf 100644 --- a/code/controllers/ModelAsController.php +++ b/code/controllers/ModelAsController.php @@ -16,9 +16,16 @@ class ModelAsController extends Controller implements NestedController { * @param string $action * @return ContentController */ - static public function controller_for(SiteTree $sitetree, $action = null) { - if($sitetree->class == 'SiteTree') $controller = "ContentController"; - else $controller = "{$sitetree->class}_Controller"; + public static function controller_for(SiteTree $sitetree, $action = null) { + if ($sitetree->class == 'SiteTree') { + $controller = "ContentController"; + } else { + $ancestry = ClassInfo::ancestry($sitetree->class); + while ($class = array_pop($ancestry)) { + if (class_exists($class . "_Controller")) break; + } + $controller = ($class !== null) ? "{$class}_Controller" : "ContentController"; + } if($action && class_exists($controller . '_' . ucfirst($action))) { $controller = $controller . '_' . ucfirst($action);