From 3eafee7799a5c4825abbbd058e22de5f697c723f Mon Sep 17 00:00:00 2001 From: Andrew Short Date: Sun, 11 Oct 2009 00:07:02 +0000 Subject: [PATCH] ENHANCEMENT: Added ModelAsController::controller_for() to link a SiteTree object to its controller. From: Andrew Short git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@88479 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/control/ModelAsController.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/core/control/ModelAsController.php b/core/control/ModelAsController.php index 53b22c669..a09c9c72b 100644 --- a/core/control/ModelAsController.php +++ b/core/control/ModelAsController.php @@ -9,6 +9,24 @@ */ class ModelAsController extends Controller implements NestedController { + /** + * Get the appropriate {@link ContentController} for handling a {@link SiteTree} object, link it to the object and + * return it. + * + * @param SiteTree $siteTree The SiteTree object to find a controller for. + * @param string $action The optional action that was requested, so that action-specific controllers work. + * @return ContentController + */ + public static function controller_for(SiteTree $siteTree, $action = null) { + $controller = "{$siteTree->class}_Controller"; + + if($action && class_exists($controller . '_' . ucfirst($action))) { + $controller = $controller . '_' . ucfirst($action); + } + + return class_exists($controller) ? new $controller($siteTree) : $siteTree; + } + public function handleRequest($request) { $this->pushCurrent(); $this->urlParams = $request->allParams();