Routes Best Practices

What are the possible locations within a Kirby app to define routes?

Follow-ups:

  • If there is more than one location (outside config.php), which is best?
  • How can we make sure they’re in a file that is checked in to version control?

I tend to add config.php to my .gitignore (primarily because of the license), so when starting to work on a new machine, I’m frequently operating without a config file for some time (until bugs start showing up, and I reconstruct it from memory, or commit pieces of it to the Readme)…

So, with this in mind— is there a spot I’m not thinking of to put the c::set('routes', []) call? I’d like it to be available in all environments. It appears as though a new file in plugins/ doesn’t get loaded in time to catch requests, so that’s out.

I’ll update this with further thoughts and questions as my own exploration continues. I appreciate your tips!

You can use the multi-environment setup

So you’ll make a config.yourdomain.com.php wich will contain your license key and you add this file to the .gitignore (and it makes perfect sense as licenses are tied to a domain)

Then you keep you config.php in your version control with everything you need (routes…)

3 Likes

Ah! So… config.php is included, then a more specific environment config file is included after?

Prior, I’d assumed Kirby only read a single config file, falling back to the main config.php if it didn’t find one matching the request/domain.

This is great. Thanks, Thomas!

That´s right. I´ve tested it a few seconds ago, because i´ve misunderstood the multi environment setup all the time. Thank you.
I also had in my mind, that only one config file is loaded, but it makes way more sense, that the domain specific files extend the general config file.
Perhaps someone can change it in the documentation to a more explicit formulation of the behavior.

You can also manually load the plugin with your routes from your config file to make sure it is loaded early:

kirby()->plugin('routes');

Since Kirby uses require_once, it won’t get loaded again later. Using a plugin is very flexible as it can have multiple files and contain other connected functionality like the corresponding handlers.

1 Like