定制 bnomei/kirby-api-pages 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

bnomei/kirby-api-pages

最新稳定版本:5.1.1

Composer 安装命令:

composer require bnomei/kirby-api-pages

包简介

Virtual Pages from APIs

README 文档

README

Kirby 5 PHP 8.2 Release Downloads Coverage Maintainability Discord Buymecoffee

Virtual Pages from APIs

Installation

  • unzip master.zip as folder site/plugins/kirby-api-pages or
  • git submodule add https://github.com/bnomei/kirby-api-pages.git site/plugins/kirby-api-pages or
  • composer require bnomei/kirby-api-pages

Usage

You can find these examples in the tests of this repository.

Records definition via Blueprint

site/models/cats.php

class CatsPage extends \Bnomei\APIRecordsPage {}

site/blueprints/cat.yml

title: Cat
fields:
    country:
        type: text
    origin:
        type: text
    coat:
        type: text
    pattern:
        type: text

site/blueprints/cats.yml

title: Cats

records:
  url: https://catfact.ninja/breeds
  query: data.sortBy("coat", "desc")
  template: cat
  # model: cat
  # expire: 60
  map:
    title: breed
    # omit or use * to select all
    # content: *
    # select a few by path
    content:
      country: country
      origin: origin
      coat: coat
      pattern: pattern

sections:
  catfacts:
    label: Virtual Pages from CatFacts API
    type: pages
    template: cat

Records definition via Config

site/blueprints/rickandmorty.yml site/blueprints/alien.yml site/blueprints/human.yml

site/models/rickandmorty.php

class RickandmortyPage extends \Bnomei\APIRecordsPage {}

site/config/config.php

<?php

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 ...
];

Records definition via Page Model

site/blueprints/secret.yml site/blueprints/secrets.yml

site/models/secrets.php

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',
                ],
            ],
        ];
    }
}

Settings

bnomei.api-pages. Default Description
expire 60 Cache expire duration in minutes
exception fn($remote) Exception handler for non 2xx status codes
records [] Custom config arrays for your virtual Pages (alternative to Blueprint config)

Disclaimer

This plugin is provided "as is" with no guarantee. You can use it at your own risk and always test it before using it in a production environment. If you find any issues, please create a new issue.

License

MIT

It is discouraged to use this plugin in any project that promotes racism, sexism, homophobia, animal abuse, violence or any other form of hate speech.

统计信息

  • 总下载量: 9
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 13
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 13
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-02-13