wizards/rest-api
Composer 安装命令:
composer require wizards/rest-api
包简介
A set of services to help you create beautiful REST APIs
README 文档
README
A framework agnostic PHP library based on fractal to help you craft beautiful REST APIs.
Goals
The main goal is to provide an easy to use and powerful REST API library that seamlessly integrates with modern frameworks.
The paradigm reads as follow:
- A method will ask for a given resource (usually a controller), sending over an http request that we Parse to find standardized parameters. A resource is a data object, that you can fetch from a data store - as a collection or as an entry. Thoses resources can have relationships, can be filtered, can be modified, ....
- The library will fetch the resource(s) according to the given Object Manager (something like an orm, odm) and request
- It then transforms the data into standardized resource(s) given the provided Object Reader (there are many ways to configure resources, such as doctrine annotations, configuration files, ...)
- It sends the found resources to fractal with the appropriate Transformer for serialization on the given output format.
Installation
composer require wizards/rest-api
Usage
The library's conventions are based on the jsonapi ones.
Query Paramters
The RestQueryParser will expect those query parameters:
-
Collection
sortnameto sort by ascending name-nameto sort by descending name- Example:
?sort=-date
filterto filter resources by values.- multiple values for filters should be provided coma-separated.
- Example:
?filter[name]=dupont,dupond&filter[surname]=thomas
filteroperator- to change the default filter opetator from
=to something else. - available operators:
<,>,<=,>=,!=,in. - Example:
?filter[age]=18&filteroperator[age]=>=
- to change the default filter opetator from
includeto include relationships data.- Example:
/books?include=author,editor
- Example:
limit: how many results you want to see by page.page: the page number. Starts at 1.
-
Single resource
includeto include relationships data. Example:/books/1?include=author,editor
Examples
Plain ol' PHP
<?php namespace App\Controller; use Psr\Http\Message\ServerRequestInterface; use WizardsRest\ObjectManager\ArrayObjectManager; use WizardsRest\CollectionManager; use WizardsRest\Provider; use WizardsRest\Serializer; use WizardsRest\ObjectReader\ArrayReader; use WizardsRest\Paginator\ArrayPagerfantaPaginator; use Symfony\Component\Routing\RouterInterface; use WizardsRest\Transformer\ArrayTransformer; Class BookController { private $source = [ ['name' => 'Book 1', 'author' => 'Author 1', 'editor' => 'Editor 1'], ['name' => 'Book 2', 'author' => 'Author 2', 'editor' => 'Editor 2'], ]; // Books controller. Somehow, this is called public function getBooks(RouterInterface $router, ServerRequestInterface $request) { // Fetch $objectManager = new ArrayObjectManager(); $paginator = new ArrayPagerfantaPaginator($router); $collectionManager = new CollectionManager($paginator, $objectManager); $collection = $collectionManager->getPaginatedCollection($this->source, $request); // Transform $fractalManager = new \League\Fractal\Manager(); $reader = new ArrayReader(); $provider = new Provider(new ArrayTransformer(), $fractalManager, $reader); $resource = $provider->transform($collection, $request, null, 'books'); // Serialize $serializer = new Serializer($fractalManager, 'https://mysite.com'); return $serializer->serialize($resource, Serializer::SPEC_DATA_ARRAY, Serializer::FORMAT_ARRAY); } }
Symfony
See the documentation on Wizards Technologies' REST API Bundle
Laravel
We are actively looking for laravel developers to support it !
Future plans
- Add more tests
- Add the sparse fieldset feature
- Add advanced filter operators such as like or between
- Optimize how the data are fetched from the source.
- Think about serialization groups.
wizards/rest-api 适用场景与选型建议
wizards/rest-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.63k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2018 年 09 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 wizards/rest-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 wizards/rest-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 7.63k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 5
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-09-20