azen / apify-lumen
一款帮助开发者轻松、快速、正确地构建RESTful API的库,即使不编写代码也可以。
Requires
- php: >=5.6.4
- php-amqplib/php-amqplib: ^2.7
- dev-master
- 1.4.9.1
- 1.4.9
- 1.4.8
- 1.4.7
- 1.4.6
- 1.4.5
- 1.4.4
- 1.4.3
- 1.4.2
- 1.4.1
- 1.3.7
- 1.3.6
- 1.3.5
- 1.3.4
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3
- 1.2.99
- 1.2.98
- 1.2.97
- 1.2.96
- 1.2.95
- 1.2.93
- 1.2.92
- 1.2.91
- 1.2.9
- 1.2.8
- 1.2.7
- 1.2.6
- 1.2.5
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.1
- 1.1.0
- 1.0.4.1
- 1.0.4
- 1.0.3.1
- 1.0.3
- 1.0.2.1
- 1.0.2
- 1.0.1
- 1.0.0
- 0.9.45
- 0.9.4
- 0.9.3
- 0.9.2
- 0.9.1
This package is not auto-updated.
Last update: 2024-10-01 20:30:59 UTC
README
一款帮助开发者轻松、快速、正确地构建 RESTful APIs
的库,即使不编写代码也可以。
它可以很容易地自定义以满足任何需求,例如定义数据关系、授权、缓存、通信或与其他系统集成。
特性
- 为任何MySql数据库提供RESTful API服务
- 分页
- 排序
- 选择
- 分组、HAVING
- 过滤
- 关系
- 元数据
- 支持事件总线
使用Apify
- Apify PHP客户端: https://github.com/megaads-vn/apify-client-php
- 使用Postman等HTTP客户端调用RESTful API。
- 与API Gateway结合使用也推荐用于构建微服务的完整开发环境。
安装
Apify被打包为composer包。因此,它可以通过以下两个步骤快速安装
-
要求composer包
composer require megaads/apify
-
注册供应商
Megaads\Apify\ApifyServiceProvider
系统需求
- PHP:>= 5.6
- Laravel/ Lumen框架:5.4.*
- MySQL
- 消息队列服务器:可选
API概述
分页
/api/post?page_id=2&page_size=20
排序
使用 sorts
参数按多个列排序
升序排序
/api/post?sorts=user_id
降序排序
/api/post?sorts=-created_at
按多个列排序
/api/post?sorts=user_id,-created_at
选择
使用 fields
参数从记录中选择列。SQL聚合函数(如 COUNT
、MAX
、MIN
、SUM
、AVG
)和SQL别名也可用
/api/post?fields=id,content,user_id,sum(view_count) as view_sum
GROUP BY
使用 groups
参数按一列或多列分组结果集,并使用 Selection
与聚合函数结合
/api/post?fields=user_id,sum(view_count)&groups=user_id
过滤
Apify支持使用逗号通过一个以上的 AND
、NOT
条件过滤记录。例如
/api/post?filters=user_id=1,status={enabled;pending},tile~hello,view_count!=null
将很快提供结合 AND
、OR
和 NOT
的复杂条件。
实体规范
Apify通过一个简单的机制工作,寻找与API实体对应的模型类,否则API实体将与合适的DB表匹配。这意味着不需要模型类来创建,只有在定义关系、自定义的情况下才这样做。
因此,API实体名称应遵循以下规范之一
-
API实体名称与模型类名称相同
-
或API实体名称为
snake_case
,对应于具有CamelCase
名称的模型类 -
或API实体名称与DB表名称相同
关系
Apify被打包为 Laravel
/ Lumen
包,因此关系也定义为 Eloquent
模型类上的方法。
有关详细信息,请参阅Laravel文档: https://laravel.net.cn/docs/5.6/eloquent-relationships
让我们考虑以下关系定义
Nation
有许多City
(一对多关系)
namespace App\Models; class Nation extends \Apify\Models\BaseModel { protected $table = 'location_nation'; public function cities() { return $this->hasMany('App\Models\City', 'nation_id', id); } }
City
属于一个Nation
(多对一关系)City
有许多District
(一对多关系)
namespace App\Models; class City extends \Apify\Models\BaseModel { protected $table = 'location_city'; public function nation() { return $this->belongsTo('App\Models\Nation', 'nation_id'); } public function districts() { return $this->hasMany('App\Models\District', 'city_id', id); } }
District
属于一个City
(多对一关系)
namespace App\Models; class District extends \Apify\Models\BaseModel { protected $table = 'location_district'; public function city() { return $this->belongsTo('App\Models\City', 'city_id'); } }
在关系上选择
Apify提供了使用 embeds
参数将关系数据嵌入结果的能力
例如
/api/nation?embeds=cities
/api/city?embeds=nation,districts
/api/district?embeds=city
甚至是嵌套关系
/api/nation?embeds=cities.districts
/api/district?embeds=city.nation
在关系上过滤
/api/city?filters=nation.location_code=EU,districts.name~land
指标
metric=get(默认):检索所有与查询匹配的记录
/api/post
或
/api/post?metric=get
响应格式
{ "meta": { "has_next": true, "total_count": 100, "page_count": 2, "page_size": 50, "page_id": 0 }, "result": [], "status": "successful" }
metric=first:检索与查询匹配的第一个记录
/api/post?metric=first
响应格式
{ "result": {}, "status": "successful" }
metric=count:检索与查询匹配的记录数
/api/post?metric=count
响应格式
{ "result": 50, "status": "successful" }
metric=increment/decrement:提供方便的方法来递增或递减所选列的值
/api/post?metric=increment&fields=view_count
响应格式
{ "result": 1, "status": "successful" }
事件总线
正在更新...
.env配置
认证Apify并授权Apify?
在此处阅读文档: https://github.com/megaads-vn/apify/blob/master/README-AUTH.md
许可证
Apify是开源软件,根据MIT许可证许可
联系我们/即时反馈
电子邮件: phult.contact@gmail.com
Skype: phult.bk
如果您发现错误,请在此处报告:Github。