Yet another plugin to provide a kind of global controller.
https://github.com/jenstornell/kirby-inherit-controller
Inherit Controller
Version 0.1
// Site controller
controllers/site.php
// Site prefixed controllers. Inherit from the site controller
controllers/site-default.php
controllers/site-projects.php
// Controllers. Does not inherit anything.
controllers/default.php
controllers/projects.php
In Kirby there there is no global controller. This plugin solves it by merging the site
controller and a site-
prefixed controller.
Maybe you need to:
- Redirect all pages that has the field value
published
. - Remove all pages from the
$pages
collection that has the slugrevision
. - Just do something globally in a controller.
Without this a plugin the above needs to be done in every controller. With many controllers it can end up with a mess.
Controller inheritance keeps things DRY.
Installation
Use one of the alternatives below.
1. Kirby CLI
If you are using the Kirby CLI you can install this plugin by running the following commands in your shell:
$ cd path/to/kirby
$ kirby plugin:install jenstornell/inherit-controller
2. Clone or download
- Clone or download this repository.
- Unzip the archive if needed and rename the folder to
inherit-controller
.
Make sure that the plugin folder structure looks like this:
site/plugins/inherit-controller/
3. Git Submodule
If you know your way around Git, you can download this plugin as a submodule:
$ cd path/to/kirby
$ git submodule add https://github.com/jenstornell/inherit-controller site/plugins/inherit-controller
Setup
1. Change your site
controller
Add /site/controllers/site.php
if it does not already exists in your installation.
<?php
return function( $site, $pages, $page ) {
return array_merge( array (
'foo' => 'Foo from site',
'bar' => 'Bar from site'
), inheritController::template( $site, $pages, $page, $args = [] )
);
};
The big difference is the array_merge
function. It merges the site
controller with the site-
prefixed controller.
2. Add a site-
custom controller
Add a controller like you normally would, but prefix it site-
.
Something like this:
<?php
return function($site, $pages, $page) {
return array(
'bar' => 'Bar from site-default'
);
};
3. Print them in the template
Add this into your /site/templates/header.php
or whatever you template is:
echo $foo; // Foo from site
echo $bar; // Bar from site-default
Nonprefixed controllers
If you add a nonprefixed controller like default.php
it will override both site.php
and site-default.php
.
That means that you can still use you templates as you normally would, until you need a change.