kelvinwongg / yapi
一个单一的声明式yaml文档CRUD API CMS框架,或者任何其他
This package is auto-updated.
Last update: 2024-09-15 07:15:42 UTC
README
一个零配置、单一声明式YAML文档、HTTP CRUD API框架。
安装
通过 composer
在项目根目录中安装YAPI
composer require kelvinwongg/yapi
用法
/api.yaml
openapi: 3.0.0 info: version: 1.0.0 title: 'HR API' paths: /employees: get: description: 'Obtain information about all employees from the HR database' parameters: - name: bodyLimit in: query required: true description: The amount of employees returned schema: type: integer minimum: 10 maximum: 20 example: 15 - name: pageLimit in: query # required: true description: The pages to return employees info schema: type: integer minimum: 1 maximum: 5 example: 2 responses: 200: description: Successful pull of employee info content: application/json: schema: $ref: '#/components/schemas/Employees' components: schemas: Employees: description: 'Array of employee info' type: array items: $ref: '#/components/schemas/Employee' Employee: description: 'Model containing employee info' type: object properties: id: type: integer example: 4 employee_name: type: string example: Ryan Pinkham employee_title: type: string example: Market Manager
/index.php
require_once __DIR__ . '/vendor/autoload.php'; $yapi = new \Kelvinwongg\Yapi\Yapi('./api.yaml');
/paths/Employees.php
<?php class Employees { public function get($file, $request, $response, $crudHook) { $response->setContent([ [ 'id' => 1, 'employee_name' => 'Steve Davis', 'employee_title' => 'Staff' ], [ 'id' => 2, 'employee_name' => 'Ronnie Hart', 'employee_title' => 'Admin' ] ]); } }
请求
curl -L -X GET 'https:///employees?bodyLimit=10'
响应
[ {"id":1,"employee_name":"Steve Davis","employee_title":"Staff"}, {"id":2,"employee_name":"Ronnie Hart","employee_title":"Admin"} ]
工作原理
-
YAPI符合OAS3规范。
-
YAPI完全是关于一个YAML文件,你给它一个YAML文件,它就会为你做事情。
-
无论是构建数据库和创建RESTful API端点,执行对数据库的正常CRUD操作。
-
你还可以将这些端点与自己的编程语言挂钩,进行任何操作,或者选择使用YAPI提供的方法响应用户。
故事
你想要快速为一些数据设置API端点。有很多人和框架可以做这件事。但问题是,作为一个专业的开发者,你想直接从事业务,而不是把你的生命浪费在重复的框架故事上
你已经花了几周或几个月的时间学习一个新的框架,阅读它的文档,测试并玩耍。然后,为了交付物,你编写了一个RESTful端点,它只是从数据库中执行CRUD操作。
事实上,这在我的职业生涯中花了很多时间(或几个月),每次我去一家新公司,只是为了简单地存储表单数据到数据库中,或者甚至没有人会费心阅读这些数据。而且大部分的研究和学习都是将新的术语或工作流程映射到你已经学过很多次的东西上。
最近我去了新工作,又走了同样的过程。这次我必须处理文档糟糕的Drupal,并尝试将其用作CMS和一些自定义JSON API的应用程序框架。生命浪费的感觉再次出现,让我想辞职。然后一个想法闪过我的脑海:为什么我不能写一个YAML文件来定义一个简单的CRUD操作的JSON API,就像我处理Docker和Kubernetes一样?