How to use the Routes in Kirby 2.2+

Hi there, I would like to know what is the correct use for multiples filters for a route in Kirby 2.2+

For URLs like…

EN: host.com/checkout/123456
ES: host.com/es/checkout/123456
  1. Must I use the following way?

     array(
       'pattern' => array(
         'checkout/(\w{6})',
         '(\w{2})/checkout/(\w{6})',
       ),
       'action'  => 'Routes::checkout'
     )
    
  2. Or?

     array(
       'pattern' => 'checkout/(\w{6})|(\w{2})/checkout/(\w{6})',
       'action'  => 'Routes::checkout'
     )
    

If there is any other way to use it, please, let me know.

Also, I would like to know if there is a way to group the routes, It would be very convenient for multi language sites.

You can make the language code optional:

array(
  'pattern' => '(?:(\w{2})/)?checkout/(\w{6})',
  'action'  => 'Routes::checkout'
)

Your solution is Stunning!! :smile:, I’m going to fix my routes just now

Anyway, I would like to know how to use the “Pipe syntax” for the routes for Kirby 2.2… If you throw me a guide, It would be great!!

Which pipe syntax are you referring to?

I’m referring to… Please check the line into change log at:

  • Option to add multiple filters to a single route by using the pipe syntax

https://github.com/getkirby/getkirby.com/commit/bdfb8ca08d397078cc59f0cd35ac24a06cfc52b8#diff-96d6b688ec01f935b6ac8320f396bc2eR93

Thanks

This line in the changelog is referring to filters, not patterns. The difference is that patterns determine whether a route matches and which parts of the URL should be extracted while filters are functions that are called before the route’s action is called. They allow to make sure a route action is only called if the user is authenticated for example. Filters are being used for the Panel and are a very specific and advanced feature.

The syntax for patterns is a regular expression and not specific to Kirby. :smile:

1 Like

If you want to use multiple patters for the same route, you can separate them by commas.

array(
    'pattern' => array('blog/(:any)', 'events/(:any)'),
    'action'  => function($uid) {
            go($uid);
    }
)

Or use regular expressions, as you already do.

As @lukasbestle explained, the pipe syntax refers to filters, to separate multiple filter, you can use a pipe |. But in your current use case, I don’t think that’s what you need.

1 Like

Thanks for you explanation, I was a little bit confused with that :grin: