Different namespaces for controllers and page models - SS4

With the advent of namespacing being used throughout Silverstripe 4, the question of having a different namespace for custom pages and their controllers has started to pop up.

By default, Silverstripe currently expects the controller and page model classes to be in the same namespace, but some developers prefer to separate them out.

If you wish to work with separate namespaces, there’s a reasonably simple way to achieve this at present. In your page model class, you can add the following method:

public function getControllerName() {
  return YourPageController::class;
}

The above assumes that you have already imported your controller’s namespace with the appropriate code (eg. use My\Namespace\Controllers\YourPageController;)

7 Likes

For additional reference, there’s some discussion around making this more flexible in core here: Controllers for SiteTree instances should be configurable · Issue #6549 · silverstripe/silverstripe-framework · GitHub