PHP code example of bnomei / kirby-api-pages

1. Go to this page and download the library: Download bnomei/kirby-api-pages library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bnomei / kirby-api-pages example snippets


class CatsPage extends \Bnomei\APIRecordsPage {}

class RickandmortyPage extends \Bnomei\APIRecordsPage {}



return [
    'bnomei.api-pages.records' => [
        'rickandmorty' => [ // site/models/rickandmorty.php & site/blueprints/pages/rickandmorty.yml
            'url' => 'https://rickandmortyapi.com/graphql', // string or closure
            'params' => [
                'headers' => function (\Bnomei\APIRecords $records) {
                    // you could add Basic/Bearer Auth within this closure if you needed
                    // or retrieve environment variable with `env()` and use them here
                    return [
                        'Content-Type: application/json',
                    ];
                },
                'method' => 'POST', // defaults to GET else provide a string or closure
                'data' => json_encode(['query' => '{ characters() { results { name status species }}}']), // string or closure
            ],
            'query' => 'data.characters.results', // {"data: [...]}
            'map' => [
                // kirby <=> json
                'title' => 'name',
                'uuid' => fn ($i) => md5($i['name']),
                'template' => fn ($i) => strtolower($i['species']), // site/blueprints/pages/alien.yml || human.yml
                'content' => [
                    'species' => 'species',
                    'hstatus' => 'status', // status is reserved by kirby
                ],
            ],
        ],
    ],
    // other options ...
];

class SecretsPage extends \Bnomei\APIRecordsPage {
    public function recordsConfig(): array
    {
        return [
            'url' => 'https://example.api/secrets', // does not exist
            'params' => [
                'headers' => [
                    'Content-Type: application/json',
                    'Authorization: Bearer MY_BEARER_TOKEN',
                ],
                'method' => 'POST',
                'data' => json_encode([
                    'query' => $this->myquery()->value(),
                ]),
            ],
            'query' => 'data.whispers',
            'template' => 'secret',
            'map' => [
                // kirby <=> json
                'title' => 'item.name',
                'content' => [
                    'description' => 'item.desc',
                    'uuid' => 'id',
                ],
            ],
        ];
    }
}