Which router syntax should I use?

Now there are at least 4 ways to register a route. Which one should we use? Why? Which ones should we stop using?

I’ve made the syntax shorter, just to show the different cases.

c::set('routes')
kirby()->routes()
$kirby->set('route')
$router->register()
  • c::set() is best used in your config.php
  • kirby()->routes() is still useful if you want to register routes from a plugin
  • $kirby->set('route') was added to make it consistent with other types of registry in Kirby 2.3
  • $router->register() is only used internally and should only be used with your own router instance

So in plugins the recommended way would still be kirby()->routes() if you want to add multiple routes at once.

2 Likes

Ahh. :slight_smile:

If I Google “kirby routes” I find this page:

https://getkirby.com/docs/developer-guide/toolkit/routing

On that page the docs is all about $router->register(). I thought it was the new cool way of doing it, but I think kirby()->routes() still looks best.

Maybe it’s just me, but I feel there are quite many syntaxes for the same thing with routes?

But I understand the reason for some of it:

  • c::set() - Consistent with the config.
  • kirby()->routes() - Consistent with other Kirby functions like kirby()->roots()->index().
  • $kirby->set('route') - Consistent with the new register set.
  • $kirby->register() - For me not consistent with anything I know of.

So in at least 3 of 4 cases it feels like all the syntaxes is there for consitency. So, is it a config, a set, a function or something else? Now it tries to be all these things and maybe it just is?

I’m not saying it’s the wrong approach, just wanted to give a heads up that there are many syntaxes that tries to make routes consistent with many things. :slight_smile:

The fourth one does not need to be consistent as it’s the low level function from the Toolkit. Only that one is available if you don’t use the CMS but just the Toolkit in your project and Kirby uses that one internally as I wrote.

I agree with @jenstornell, at the moment there are – in my opinion – to much possibilities for one same thing. Especially for new developers this could look somehow messy, I’d guess. Personally, I never went with the config-option for example, as this is clearly no option for me (at least in the way routes are registered right now – when using e.g. the redirect plugin from Jens, the config is the best place).

Another thing – are there any chances that a route gets overwritten when using different syntaxes for the same route? If so, it might be good to be aware of this and give hints which syntax is the most “powerful” one… and maybe think about ditching one or two options for a new version, even if this would lead to some broken sites (but I guess most of us are always changing some things and implementing the new cool stuff when updating their sites, so this might be not a big problem?)

1 Like

I had my routes in the following order:

$kirby->set('route');
kirby()->routes();

Still, the kirby()->routes() was run first so I guess that one is more “powerful” then.