From 8206538f31eba6e29a6d84a5b1707f7ac4a458ce Mon Sep 17 00:00:00 2001 From: Adrian Humphreys Date: Fri, 26 Apr 2019 14:22:04 +1200 Subject: [PATCH] Add info around namespacing with controllers --- .../02_Controllers/01_Introduction.md | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/docs/en/02_Developer_Guides/02_Controllers/01_Introduction.md b/docs/en/02_Developer_Guides/02_Controllers/01_Introduction.md index e631a7ee0..458dd6bf2 100644 --- a/docs/en/02_Developer_Guides/02_Controllers/01_Introduction.md +++ b/docs/en/02_Developer_Guides/02_Controllers/01_Introduction.md @@ -178,6 +178,46 @@ public function Link($action = null) } ``` +## Connecting Pages to Controllers +By default, a controller for a page type must reside in the same namespace as its page. If you find that your controllers are in a different namespace then you'll need to overide SiteTree::getControllerName(). + +Example controller: +```php +namespace App\Controller; + +use SilverStripe\Control\Controller; + +class TeamPageController extends Controller +{ + public function getExample() + { + return 'example'; + } +} +``` + +Example page: +```php +namespace App\Page; + +use App\Controller\TeamPageController; +use Page; + +class TeamPage extends Page +{ + public function getControllerName() + { + return TeamPageController::class; + } +} +``` +You'd now be able to access methods of the controller in the pages template + +```html + +

{$getExample}

+``` + ## Related Lessons * [Controller actions/DataObjects as pages](https://www.silverstripe.org/learn/lessons/v4/controller-actions-dataobjects-as-pages-1) * [Creating filtered views](https://www.silverstripe.org/learn/lessons/v4/creating-filtered-views-1)