tbbc/rest-util
Composer 安装命令:
composer require tbbc/rest-util
包简介
Some useful classes for handling certain aspects of a REST(ful) API.
README 文档
README
Rest util library for PHP.
This library implement some useful util classes for resolving recurrent issues in Rest(ful) APIs (i.e: Error handling).
Quick Start
Table of contents
Description
Rest utilitary library for PHP
Installation
Using Composer, just $ composer require tbbc/rest-util package or:
{ "require": { "tbbc/rest-util": "dev-master" } }
Usage
Converting Exception into Error object:
1. First you have to define an ExceptionMapping, for the sake of the example,
we use PHP, but a YamlLoader is also available:
// Exception mapping configuration $invalidArgumentExceptionMapping = new ExceptionMapping(array( 'exceptionClassName' => '\InvalidArgumentException', 'factory' => 'default', 'httpStatusCode' => 400, 'errorCode' => 400101, 'errorMessage' => null, 'errorExtendedMessage' => 'Extended message', 'errorMoreInfoUrl' => 'http://api.my.tld/doc/error/400101', ));
2. The ExceptionMapping must be added to ExceptionMap:
$exceptionMap = new ExceptionMap(); $exceptionMap->add($invalidArgumentExceptionMapping);
3. We plug the ErrorResolver with an ErrorFactory, a DefaultErrorFactory is bundled within the lib:
// Error resolver initialization $errorResolver = new ErrorResolver($exceptionMap); $defaultErrorFactory = new DefaultErrorFactory(); $errorResolver->registerFactory($defaultErrorFactory);
4. Resolve error!
$exception = new \InvalidArgumentException('This is an invalid argument exception'); $error = $errorResolver->resolve($exception);
5. The $error variable is now assigned an ErrorInterface object which implements a toArray() method
to allow easy serialization method of your choice:
print_r($error->toArray()); /* will output Array ( [http_status_code] => 400 [code] => 400101 [message] => This is an invalid argument exception. [extended_message] => Extended message [more_info_url] => http://api.my.tld/doc/error/400101 ) */ echo json_encode($error->toArray()); /* And voilà! You get a ready to use json normalized error response body { "http_status_code": 400, "code": 400101, "message": "This is an invalid argument exception.", "extended_message": "Extended message", "more_info_url": "http:\/\/api.my.tld\/doc\/error\/400101" } */
For more concrete real life usage, take a look at our bundle for integrating the lib into a Symfony application: TbbcRestUtilBundle.
Run the test
First make sure you have installed all the dependencies, run:
$ composer install --dev
then, run the test from within the root directory:
$ vendor/bin/phpunit
Contributing
- Take a look at the list of issues.
- Fork
- Write a test (for either new feature or bug)
- Make a PR
Requirements
- PHP 5.3+
Authors
- Boris Guéry - guery.b@gmail.com - http://borisguery.com
- Benjamin Dulau - benjamin.dulau@gmail.com
License
The Big Brains Company - Rest Util is licensed under the MIT License - see the LICENSE file for details
tbbc/rest-util 适用场景与选型建议
tbbc/rest-util 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 222.11k 次下载、GitHub Stars 达 16, 最近一次更新时间为 2013 年 08 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「rest」 「api」 「restful」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tbbc/rest-util 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tbbc/rest-util 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tbbc/rest-util 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PSR-7 compatible library for making CRUD API endpoints
Api bundle
This is a restful api to onesignal.
A RESTful API package for the Laravel and Lumen frameworks.
Simple and lightweight HTTP client based cURL
A Flickr wrapper to allow you to call the Flickr api with Guzzle as the backend.Goal is to have 100% Flickr api coverage rather than just upload/display photos (currently at 23%).
统计信息
- 总下载量: 222.11k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 16
- 点击次数: 11
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2013-08-28