Dynamic routes based on config options array

Overview

I have a multi-site setup consisting of three individual sites. Each of these sites has its own configuration file that includes a 'feeds' option. This option specifies the feeds where events can be fetched by my event plugin.

The 'feeds' option looks like this in the config.php:

config.php

  'feeds' => [
    'https://feed-eins.test',
    'https://feed-zwei.test'
  ],

Objective

Currently, my event plugin exports all events that have the “export” flag set to true, regardless of the feed they belong to. Now, I want to customize this behavior. I want to filter events based on the feed URLs (‘feed-eins’ and ‘feed-zwei’) stored in the $receivingsites field of each event.

The $receivingsites field contains a comma-separated string of feed URLs, like so: 'https://feed-eins.test, https://feed-zwei.test'.

Current Implementation

In the current state of my event plugin’s index.php, I have a route defined for ‘/export/events’. This route fetches and exports all events that have their “export” flag set to true. It does not yet differentiate between the two feeds.

'routes' => [
    [
      'pattern' => '/export/events',
      'method' => 'GET',
      'action' => function(){
        $events = site()->index()->filterBy('intendedTemplate', 'event')->filterBy('export', 'true');
        $data = [];

        foreach($events as $event) {
          $data[] = [
            'slug' => (string)$event->slug(),
            'receivingsites' => (string)$event->receivingsites(),
          ];
        }

        return Kirby\Http\Response::json($data, 200, true);
      }
    ],

you can use a closure that has kirby instancd and make that return the routes array.

'routes' => function($kirby) {
           $newsUri = $kirby->option("my.plugin.slug", "news");
           
           return [
               [
                   'pattern' => "{$newsUri}/(:any)",
                   'action' => function($any) {
                       if(!empty($any))
                       {
…