luyadev / luya-headless
LUYA 无头 API 客户端
2.10.1
2023-09-13 08:46 UTC
Requires
- curl/curl: ^2.0
- luyadev/yii-helpers: ^1.4
- psr/simple-cache: ^1.0
Requires (Dev)
Suggests
- symfony/cache: A very fast PSR-16 caching library easy to setup for caching API responses.
README
LUYA 无头客户端
一个客户端库,用于访问 LUYA API(或任何其他 REST API)的内容。
安装
将 LUYA 无头客户端库添加到您的 composer.json 文件中
composer require luyadev/luya-headless
简介
关于如何使用自定义端点使用无头库的快速简介:创建 Api 类(这与 Active Record 模式非常相似)
class ApiCars extends \luya\headless\ActiveEdnpoint { public $id; public $name; public $year; public function getEndpointName() { return '{{api-cars}}'; } }
有了新的 ApiCars 类,您现在可以插入、更新或检索数据
use luya\headless\Client; // build client object with token and server infos $client = new Client('API_TOKEN', 'http://localhost/luya-kickstarter/public_html'); // create new value $car = new ApiCars(); $car->name = 'BMW'; $car->year = 2019; $car->save($client); // find a given user by its ID $car = ApiCars::viewOne(1, $client); echo $car->name; // BMW echo $car->year; // 2019 // update an existing value $car->year = '2018'; $car->save($client); // iterate all cars $users = ApiCars::find()->setSort(['id' => SORT_ASC])->all($client); foreach ($users->getModels() as $car) { echo $car->name; }
文档
请参阅完整文档,了解如何进行 put、delete 或 post 请求,处理分页或访问 CMS 块。
开发和贡献
- PSR 命名规范:https://www.php-fig.org/bylaws/psr-naming-conventions/
- 缓存组件需求:https://www.php-fig.org/psr/psr-16/(示例实现,使用:https://github.com/symfony/cache
new FilesystemCache('', 0, 'path/to/cache/folder');
) - 单元测试
composer install
并运行./vendor/bin/phpunit tests