Move api endpoint creation from controller to router


#1

hi!

this is more of a can i do it simpler and more leaner kind of question:

i setup a controller (api.php) + template (api.php, api.json.php) + content/page (/api) to create an API endpoint where kirby is fetching some data from another external service. this all works.

i was wondering, and had some vague memories, that maybe this might be all possible to do by using only a router in config.php? or not?

  • if yes, is it allowed to call a php library from within config.php?
  • if not, then maybe i can still use a route and being able to ditch /template/api.php and /template/ap.json.php and only keep the controller?

thanks!
af


#2

You can do it in a route, yes. But I wouldn’t put the route into the config.php but into a plugin. then you can have all your logic in the plugin. If you don’t have a template, then you don’t want to use a controller.