Multiple custom routes with Kirby instances

Hi there

I have recently setup a custom endpoint where I need a kirby instance. This looked like this:

<?php

return [
  'debug' => true,
  'api' => [
    'slug' => 'api',
    'basicAuth' => true,
    'allowInsecure' => true
  ],
  'routes' => [
    // Custom route to get all necessary archive info, which is directly saved within the entry page
    [
      'pattern' => 'archive',
      'method' => 'GET',
      'action'  => function ($path = null) {
        // we need to create a new kirby instance to get access to all the kirby methods (like resize(), getting content etc.)
        $kirby = new Kirby([
          'roots' => [
              'index'    => (dirname(__DIR__)),
              'base'     => $base    = dirname(__DIR__, 2),
              'site'     => $base . '/site',
              'storage'  => $storage = $base . '/storage',
              'content'  => $storage . '/content',
              'accounts' => $storage . '/accounts',
              'cache'    => $storage . '/cache',
              'media'    => $storage . '/media', // NOTE: needs symlink /public/media to /storage/media
              'sessions' => $storage . '/sessions',
          ]
        ]);
        $children = $kirby->page('archive')->children();
        $entries = [];

        foreach ($children as $child) {
          if ($child->status() !== 'listed') {
            continue;
          }

          $title = $child->title()->toString();
          $uri = $child->uri();
          $content = $child->content();
          $index = $child->indexOf();
          $images = [];


          $entries[] = [
            'title' => $title,
            'slug' => $uri,
            'index' => $index,
            'uri' => $child->uri(),
            'slug' => $child->slug(),
            'file' => $child->filename()->toString(),
            'date' => $child->content()->date()->toString(),
            'end_time' => $child->content()->end_time()->toString()
          ];
        }

        return [
          'archive_entries' => $entries
        ];
      }
    ]
  ]
];

Now I need a second custom route and I don’t want to repeat the code where I create a new kirby instance.
I thought I could just define $kirby before returning routes, or create a function that returns a new kirby instance, but all of results in

Fatal error: Uncaught Error: Maximum function nesting level of ‘256’ reached, aborting!

For example I now tried:

return [
  'debug' => true,
  'api' => [
    'slug' => 'api',
    'basicAuth' => true,
    'allowInsecure' => true
  ],
  'routes' => function() {
    $kirby = new Kirby([
      'roots' => [
          'index'    => (dirname(__DIR__)),
          'base'     => $base    = dirname(__DIR__, 2),
          'site'     => $base . '/site',
          'storage'  => $storage = $base . '/storage',
          'content'  => $storage . '/content',
          'accounts' => $storage . '/accounts',
          'cache'    => $storage . '/cache',
          'media'    => $storage . '/media', // NOTE: needs symlink /public/media to /storage/media
          'sessions' => $storage . '/sessions',
      ]
    ]);

    return [
      [
        // Custom route to get all necessary archive info, which is directly saved within the entry page
        'pattern' => 'archive',
        'method' => 'GET',
        'action'  => function ($path = null) {
          $children = $kirby->page('archive')->children();
          $entries = [];

          foreach ($children as $child) {
            if ($child->status() !== 'listed') {
              continue;
            }

            $title = $child->title()->toString();
            $uri = $child->uri();


            $entries[] = [
              'title' => $title,
              'slug' => $uri,
              // etc.
            ];
          }

          return [
            'archive_entries' => $entries
          ];
        }
      ]
    ];
  }
];

I also tried to pass in $kirby as param:
'action' => function ($path = null, $kirby) { but no success either…

How could I write this in a more elegant way than repeating the kirby instance part, and what is the actual problem?
Sorry, maybe this is just my weakness in php, but I would be super happy to hear your input.
Cheers

Try setting your routes in a plugin instead of in the config.

Also you can always get the current Kirby instance inside your routes using the kirby() helper. You don’t need to (and shouldn’t) create your own instance.