Inherit Controller - Controllers that inherit from the site controller

That would probably not work but now I see what you mean. The idea with some modifications would probably work but then you need to add that include and compact (if that works) to every controller that needs to inherit the site controller.

Here is my plugin, yes it’s the whole thing :slight_smile: :

You don’t need to understand this to use it but maybe you want some insight in how it works.

<?php
class inheritController {
	public static function template( $site, $pages, $page, $args = [] ) {
		$controller = kirby()->registry()->get( 'controller', 'site-default' );
		if( is_a( $controller, 'Closure' ) ) {
			return (array)call_user_func_array( $controller, array(
				$site,
				$pages,
				$page,
				$args,
			));
		}
		return array();
	}
}

site.php

The magic with my plugin is to add a function to the site controller, instead of every other controller. So this is the new site controller:

<?php
return function( $site, $pages, $page ) {
  return array_merge( array (
    'foo' => 'Foo from site',
    'bar' => 'Bar from site'
      ), inheritController::template( $site, $pages, $page, $args = [] )
  );
};

site-projects.php

If you template/controller name is normally projects you can use a prefixed version site-projects.php. No includes needed. Magically it merges it with the site controller.

<?php
return function($site, $pages, $page) {
  return array(
    'bar' => 'Bar from site-default'
  );
};