How to create a panel plugin API endpoint?

I’m creating a plugin that should be able to export the site content based on some filters. I can register a route like:

Kirby::plugin('hdodov/myplugin', [
  'routes' => function () {
    return [
      [
        'pattern' => 'myplugin/export',
        'action' => function () {
          // export logic
        }
      ]
    ];
  }
]);

However, that’s available at http://localhost/myplugin/export.

I think it would be better if my endpoint begins with panel. However, if I change the pattern to panel/myplugin/export, the action is not triggered and instead, I see the panel itself and get redirected to panel/site. Same happens if I set the action to panel/plugins/myplugin/export. After all, panel/plugins/myplugin is where my plugin view resides. It’s kind of the URL namespace of my plugin. It makes sense to be able to add some routes there? :thinking:

I would go for custom API routes in this case: https://getkirby.com/docs/guide/api/introduction#custom-api-routes

1 Like