Fetch data through a route/plugin or api?

I am quite new to Kirby and building my first project based on this great vuejs kirby starterkit.

I have a rather conceptual question for an advice on how to fetch data from kirby. I am fetching all the tags through the json representation of a template page inside my vue component. By clicking on one of the tags I would like to display how many kirby pages are associated with the current tag.

Should I create a plugin and do the “logic” in php or should I create a specific route in config.php or use the API? Or should I do the entire logic in Vue but it seems that would just replicate the functionality that’s already available through Kirby…

I did a quick test with the plugin approach but I don’t have access to the pages object which makes things complicated.

Thanks for hints/advices.

When I am creating a plugin to fetch for example the site children I am getting this error:

Error thrown with message "Call to a member function children() on null"

This is the plugin code:

<?php

  Kirby::plugin('myplugin/route', [
  'routes' => [
    [
        'pattern' => 'tagCount/(:any)',
        'action' => function ($query) {
            return site()->find($query)->children();
        }
    ]
  ]
]);
?>

This needs an if statement to make sure the page exists before calling children().

Indeed, I made sure that the page exists and the error message changed. Unfortunately, it’s less obvious what’s going wrong. I am receiving following error:

Kirby \ Exception \ InvalidArgumentException (error.invalidArgument)

Unexpected input

You also have to return the collection as array:

<?php

Kirby::plugin('myplugin/route', [
    'routes' => [
        [
            'pattern' => 'tagcount/(:any)',
            'action' => function ($query) {
                if ($page = site()->find($query)) {
                    return $page->children()->toArray();
                }
                return [];
            }
        ]
    ]
]);
1 Like