getkirby / ideas

This is the backlog of ideas and feature requests from the last two years. Use our new feedback platform to post your new ideas or vote on existing ideas.
https://feedback.getkirby.com
20 stars 0 forks source link

API Endpoint to list all pages #565

Closed ryami333 closed 4 years ago

ryami333 commented 4 years ago

Hi there, could we please have a new Rest API endpoint (GET "/pages") to list all pages?

My use-case is static-site-generation, where all data needs to be known ahead of time. For my use-case, I need a "parent lookup table", and I can't build that without being able to query all of the pages dynamically (ie. without knowing their specific slugs). Would also be necessary for generating sitemaps, building main-menus etc etc. Necessary for all the same reasons as the existence of the $pages variable in Kirby.

lukasbestle commented 4 years ago

Do you mean the recursive $site->index() of all pages? This is currently not supported by the API and to be honest I don't think it will be added soon (few use-cases considering the low performance of such a lookup). However you have two options:

bastianallgeier commented 4 years ago

@lukasbestle already pointed out some good options.

Your own API route is very simple to create for this use-case:

// site/plugins/index-api/index.php
Kirby::plugin('ryami333/index-api', [
    'api' => [
        'routes' => [
            [
                'pattern' => 'pages',
                'method'  => 'GET',
                'action'  => function () {
                    return $this->site()->index();
                }
            ]
        ]
    ]
]);

This will create a new /api/pages endpoint that will give you all pages at once.