Using filters within the Router

So, I saw that Kirby 2.5.3 fixes some things with filters and routes. It is now possible to use a routing filter for just one route. My question is: how do I create a global filter?

The examples on that page all start with a $router variable, which is defined at the top as $router = new Router();. But you can’t just create a new Router in your plugin or config.php file, because within $kirby->launch(), $kirby->router is set to a new Router.

This happens after plugins are loaded, so we can set routes with kirby()->set('route', []); within plugin files, but there is no way to get a global filter in.

Or am I missing something? :slight_smile:

If you define the filter in a function, you can use it like this it seems:

<?php
function authenticateUser() {
  if(!$user = site()->user()){
    go('error');
  }
}

kirby()->set('route',
  array(
    'pattern' => 'api/test',
    'action'  => function() {
      return response::json(array(
        'some', 'json', 'stuff'
      ));
    },
    'filter'  => function() {
      authenticateUser();
    },
    'method'  => 'GET|POST'
  )
);

Yeah, that works too. My point was more that there is a nice Kirby way of doing it with the toolkit, but that we can’t actually use that in Kirby itself because it’s not exposed.

Edit, maybe I should’ve labeled it as “Suggestion” then, sorry 0:)