Including own helper classes

What is the best solution to include own written helper classes into kirby cms to get access to it on snippets, themes, controllers etc. – something like the kirby toolkit, which is available nearly everywhere.

Right now I am requiring php files to the config.php – but I think this might not be the best thing :wink:

Thanks in advance for your suggestions!

I think you should use the plugin folder for this.

By doing this your function is accessible from anywhere.

A good starting point is to create a simple helper function in your plugin, which you can then start to use immediately in all your templates, snippets and controllers.


Thanks for the suggestion. Is there something like a visual hierarchy when things are fired in kirby? Like the template hierarchy in Wordpress? Are Plugins fired before the config? When are classes available when I’m writing them into a plugin?

1 Like

Not sure about this one. I just would guess that the config is loaded first but I don’t got lot’s of insight into the deeper structure of the kirby call hierarchy.

Why do you need to know when things are called?

Maybe @lukasbestle or @texnixe can you answer this more precisely :slight_smile:

1 Like

I’m totally in love with the cms and I’m really addicted to it.

Right now I am developing several things with it and would like to test out how deep I can go with this. The hierarchy helps a little bit with some of my ideas. So if there’s something available, it would be nice to know it… :wink:

Here’s the kirby launch function that is called in index.php, as you can see, the configuration is called first ($this->site()), then after forcing ssl if applicable and setting the date, the kirby extensions, then the plugins etc.

    // this will trigger the configuration
    $site = $this->site();

    // force secure connections if enabled
    if($this->option('ssl') and !r::secure()) {
      // rebuild the current url with https
      go(url::build(array('scheme' => 'https')));

    // set the timezone for all date functions

    // load all extensions

    // load all plugins

    // load all models

    // start the router
    $this->router = new Router($this->routes());
    $this->route  = $this->router->run($this->path());

    // check for a valid route
    if(is_null($this->route)) {
        'status'  => 'error',
        'message' => 'Invalid route or request method'

    // call the router action with all arguments from the pattern
    $response = call($this->route->action(), $this->route->arguments());

    // load all language variables
    // this can only be loaded once the router action has been called
    // otherwise the current language is not yet available

    // build the response
    $this->response = $this->component('response')->make($response);

    // store the current language in the session
    if($this->site()->multilang() && $language = $this->site()->language()) {
      s::set('language', $language->code());

    return $this->response;


Hope this helps.
1 Like

This helps really alot!!! :+1:

So maybe requiring php files in config.php might really the best thing to have own classes accessible as early as possible.

What is your use-case for loading plugins that early? Do you need access to the classes in the config file already? And if so, what exactly are you trying to do?

First thing is: I need to set constants to have them available everywhere in my cms. Instead of defining them in an include file I want to code them directly into the firing process. So it’s good to know for me now that it might even be better to code them directly into the index.php.

What kind of constants?

Special routes to my own app folders. I know to what kind of answer your question targets :wink:

So no $roots->foo() :nerd:

Well, it interests me what you are trying to do so that I can tell you what the best way would be.

Can’t you just set those constants in config.php? You will have access to them in any plugins, in the templates, routes etc.

i am using composer a lot. to autoload stuff i include it in a custom site.php. i also include my custom helper classes there.
but putting them in a plugin sounds like a good idea too.

1 Like