aejnsn/lapis
Composer 安装命令:
composer require aejnsn/lapis
包简介
An API toolkit for Laravel 5.5+
关键字:
README 文档
README
A toolkit building on API Resources in Laravel.
Concept
RESTful API endpoints typically require more functionality than pagination alone. For example, a front-end or mobile application may implement a filtering scheme or require access to the endpoint's nested resources. Lapis intends to provide a lightweight filtering and nested resource pattern leveraging the existing facilities in Laravel 5.5.
Includes
Let's say we have an endpoint in a blogging platform to list our post resources returning a response like so:
GET https://api.myrecipeblog.com/posts
{
"posts": [
{
"name": "Asparagus Steak Oscar",
"category": "food",
"postedAt": "2017-09-12 16:04:33",
"authorId": 14
},
{
"name": "Churchill Downs Mint Julep",
"category": "drinks",
"postedAt": "2017-10-05 18:32:12",
"authorId": 15
}
]
}
Ideally we would want to see details of a post's author in our front-end.
Right, so make another request to a hypothetical users endpoint referencing a distinct list of authorIds from the posts response...No, absolutely not!
We (hopefully) spent the time in our blogging platform's backend to model our data's relationships and set up foreign key constraints with appropriate indices.
So let's put those models to work.
Lapis, leveraging Laravel's API Resources, allows us to add an include parameter on our request URL to retrieve the nested author (User) relationship.
Our request URL and response would look something like this:
GET https://api.myrecipeblog.com/posts?include=author
{
"posts": [
{
"name": "Asparagus Steak Oscar",
"category": "food",
"postedAt": "2017-09-12 16:04:33",
"authorId": 14,
"author": {
"id": 14,
"name": "John Doe",
"forHire": true,
"chefRating": 5
}
},
{
"name": "Churchill Downs Mint Julep",
"category": "drinks",
"postedAt": "2017-10-05 18:32:12",
"authorId": 15,
"author": {
"id": 15,
"name": "James Smith",
"forHire": false,
"chefRating": 1
}
}
]
}
Nested Includes
This will also work for nested relationships.
For example, if our User model contained a favorites relationship we could request
GET https://api.myrecipeblog.com/posts?include=author.favorites
and an array of the author's favorites would be nested inside the author object.
Filtering
Let's go back to our original response above, for which we called the posts endpoint.
Maybe we only want to see posts from the drinks category.
We could alter our call from above like so:
GET https://api.myrecipeblog.com/posts?filter[category]=drinks
{
"posts": [
{
"name": "Churchill Downs Mint Julep",
"category": "drinks",
"postedAt": "2017-10-05 18:32:12",
"authorId": 15
}
]
}
We can also specify multiple filters using this structure:
GET https://api.myrecipeblog.com/posts?filter[category]=drinks&filter[authorId]=15
Nested Filters
We discussed including an author relationship above.
We can also filter on the author's details by making a request like so:
GET https://api.myrecipeblog.com/posts?filter[author.forHire]=true
Filter Operators
Up until this point we've just used filters to assert a field is equal to a given value. Filters understand the concept of operators. Here are a few examples:
List posts later than October 1, 2017.
GET https://api.myrecipeblog.com/posts?filter[postedAt{gte}]=2017-10-01
List posts where the author's name starts with John.
GET https://api.myrecipeblog.com/posts?filter[author.name{starts}]=John
Reference Implementation
Installation & Usage
aejnsn/lapis 适用场景与选型建议
aejnsn/lapis 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19 次下载、GitHub Stars 达 2, 最近一次更新时间为 2017 年 10 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「toolkit」 「laravel」 「filtering」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 aejnsn/lapis 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 aejnsn/lapis 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 aejnsn/lapis 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PSR-7 compatible library for making CRUD API endpoints
Symfony2 Bundle that adds basic user account entity and city/state/country setup as well
Common helpers and classes for ICanBoogie
Alfabank REST API integration
Enable use of agile toolkit framework in wordpress
Handy PHP Utility library
统计信息
- 总下载量: 19
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-10-16
