Kirby Init Controller

Anyone remeber my Kirby Pre Controller? It was a quite poor attempt to solve a real problem. It required extra code in the header.php file.

Require Kirby 2.3 beta 2

Init controller

https://github.com/jenstornell/kirby-init-controller

This is an extra controller that will run before the template starts.

  • It works just like any other controller.
  • It runs on every page.
  • It runs after all other controllers has been loaded.

The beauty of it is that it’s using the template component which means that it has none of the pitfalls that the Kirby Pre Controller had.

This time I think it’s a real solution, not just a hackish concept. I think the code is pure beauty.

Only downside is now you can’t use an alternative template component. :slight_smile:
(Most people will have no problem with that, so it’s probably not an issue.)

For the record I mostly solved my “code that runs on every request” needs with:

  • one or more scripts in site/plugins
  • adding a few Page methods and one or two methods to the Site class too (rather than having a global controller that sends $customData to each template, I opted for using a $site->customData() method that does the same thing; it’s a nice way to expose utility functions).

Of course a global or init controller is a valid approach too.

Only downside is now you can’t use an alternative template component.

Yes, I know. But this problem will happen to any plugin that is using this component. The component is kind of locked to one plugin. That’s because there is no queue system in Kirby.

Version 0.3

github.com/jenstornell/kirby-init-controller

Before it was just an init controller, now I added a “pre controller” as well. The reason for it is that my routes feels a bit bloated.

Summery

Pre controller

This is an extra controller that will run before the built in controller starts.

  • It works just like any other controller.
  • It runs on every page.
  • It runs before the normal controller has been loaded.
  • All the normal controllers has access to the pre controller data, because it’s global.

Init controller

This is an extra controller that will run before the template starts.

  • It works just like any other controller.
  • It runs on every page.
  • It runs after all other controllers has been loaded.
  • All the templates and snippets has access to the init controller data, because it’s global.