ellipsesynergie/api-response
Composer 安装命令:
composer require ellipsesynergie/api-response
包简介
Simple package to handle response properly in your API
README 文档
README
Simple package to handle response properly in your API. This package uses Fractal and is based on Build APIs You Won't Hate book.
Install
Via Composer
$ composer require ellipsesynergie/api-response
Requirements
The following versions of PHP are supported by this version:
>= PHP 8.1
Install in Laravel
Add this following service provider to your config/app.php file.
EllipseSynergie\ApiResponse\Laravel\ResponseServiceProvider::class
Install in Lumen
Because of the request object change (see reference) you can no longer access Request object properly in Service provider. To be convenient, we have created a middleware to be used for parsing the include parameter.
Register this service provider to your bootstrap/app.php file.
$app->register('EllipseSynergie\ApiResponse\Laravel\LumenServiceProvider');
Register the global middleware bootstrap/app.php file.
$app->middleware([ 'EllipseSynergie\ApiResponse\Laravel\Middleware\ParseInclude' ]);
Install in your favorite framework or vanilla php
This package can be used in any framework or vanilla php. You simply need to extend EllipseSynergie\ApiResponse\AbstractResponse and implement the withArray() method in your custom class.
You can take a look at EllipseSynergie\ApiResponse\Laravel\Response::withArray() for an example.
You will also need to instantiate the response class with a fractal manager instance.
// Instantiate the fractal manager $manager = new \League\Fractal\Manager; // Set the request scope if you need embed data $manager->parseIncludes(explode(',', $_GET['include'])); // Instantiate the response object, replace the class name by your custom class $response = new \EllipseSynergie\ApiResponse\AbstractResponse($manager);
For more options related to the fractal manager, you can take a look at the official Fractal website
Example inside Laravel or Lumen controller
<?php use EllipseSynergie\ApiResponse\Contracts\Response; class BookController extends Controller { /** * @param Response $response */ public function __construct(Response $response) { $this->response = $response; } /** * Example returning collection */ public function index() { //Get all books $books = Book::all(); // Return a collection of $books return $this->response->withCollection($books, new BookTransformer); } /** * Example returning collection with custom key */ public function index() { //Get all books $books = Book::all(); //Custom key $customKey = 'books'; // Return a collection of books return $this->response->withCollection($books, new BookTransformer, $customKey); } /** * Example returning collection with paginator */ public function index() { //Get all books $books = Book::paginate(15); // Return a collection of $books with pagination return $this->response->withPaginator( $books, new BookTransformer ); } /** * Example returning collection with paginator with custom key and meta */ public function index() { //Get all books $books = Book::paginate(15); //Custom key $customKey = 'books'; //Custom meta $meta = [ 'category' => 'fantasy' ]; // Return a collection of $books with pagination return $this->response->withPaginator( $books, new BookTransformer, $customKey, $meta ); } /** * Example returning item */ public function show($id) { //Get the book $book = Book::find($id); // Return a single book return $this->response->withItem($book, new BookTransformer); } /** * Example returning item with a custom key and meta */ public function showWithCustomKeyAndMeta($id) { //Get the book $book = Book::find($id); //Custom key $customKey = 'book'; //Custom meta $meta = [ 'readers' => $book->readers ]; // Return a single book return $this->response->withItem($book, new BookTransformer, $customKey, $meta); } /** * Example resource not found */ public function delete($id) { //Try to get the book $book = Book::find($id); //Book not found sorry ! if(!$book){ return $this->response->errorNotFound('Book Not Found'); } } /** * Example method not implemented */ public function whatAreYouTryingToDo() { return $this->response->errorMethodNotAllowed("Please don't try this again !"); } }
Ouput example
One book
{
"data": {
"id": 1,
"title": "My name is Bob!.",
"created_at": {
"date": "2014-03-25 18:54:18",
"timezone_type": 3,
"timezone": "UTC"
},
"updated_at": {
"date": "2014-03-25 18:54:18",
"timezone_type": 3,
"timezone": "UTC"
},
"deleted_at": null
}
}
Collection of books
{
"data": [
{
"id": 1,
"title": "My name is Bob!",
"created_at": {
"date": "2014-03-25 18:54:18",
"timezone_type": 3,
"timezone": "UTC"
},
"updated_at": {
"date": "2014-03-25 18:54:18",
"timezone_type": 3,
"timezone": "UTC"
},
"deleted_at": null
},
{
"id": 2,
"title": "Who's your dady ?",
"created_at": {
"date": "2014-03-26 18:54:18",
"timezone_type": 3,
"timezone": "UTC"
},
"updated_at": {
"date": "2014-03-26 18:54:18",
"timezone_type": 3,
"timezone": "UTC"
},
"deleted_at": null
}
]
}
Error
{
"error": {
"code": "GEN-NOT-FOUND",
"http_code": 404,
"message": "Book Not Found"
}
}
Testing the package
$ phpunit
Testing within Laravel
According to the issue #31, we have found some problem when it's time to test the include query parameter value.
If you want to resolve this issue in your test, you must use the trait EllipseSynergie\ApiResponse\Testing\Laravel\AddTestingSupportForInclude. To replace the call method from Illuminate\Foundation\Testing\Concerns\MakesHttpRequests::call
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.
ellipsesynergie/api-response 适用场景与选型建议
ellipsesynergie/api-response 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.4M 次下载、GitHub Stars 达 374, 最近一次更新时间为 2014 年 03 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「json」 「api」 「fractal」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ellipsesynergie/api-response 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ellipsesynergie/api-response 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ellipsesynergie/api-response 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
CakePHP view builder utilizing Fractal library for entities transformation
Kinikit - PHP Application development framework MVC component
Wraper for spatie fractal package
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
A PSR-7 compatible library for making CRUD API endpoints
统计信息
- 总下载量: 1.4M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 377
- 点击次数: 23
- 依赖项目数: 22
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-03-25