rcm / api-lib
Rcm Api 库
0.12.1
2019-04-16 16:14 UTC
Requires
Requires (Dev)
Suggests
- bower-asset/angular: Required for JS
- bower-asset/es6-promise-polyfill: ^1.2 for older browser support
- rcm/i18n: Required by default, can over-ride in config: Translate::class
- rwoverdijk/assetmanager: Required if using asset manager config
- zendframework/zend-http: Required if using ZF2 controllers
- zendframework/zend-mvc: Required if using ZF2 controllers
- zendframework/zend-stdlib: Required if using ZF2 controllers
- dev-master
- 0.12.1
- 0.12.0
- 0.11.2
- 0.11.1
- 0.11.0
- 0.10.3
- 0.10.2
- 0.10.1
- 0.10.0
- 0.9.7
- 0.9.6
- 0.9.5
- 0.9.4
- 0.9.3
- 0.9.2
- 0.9.1
- 0.9.0
- 0.8.3
- 0.8.2
- 0.8.1
- 0.8.0
- 0.7.2
- 0.7.0
- 0.6.1
- 0.6.0
- 0.5.9
- 0.5.8
- 0.5.7
- 0.5.6
- 0.5.5
- 0.5.4
- 0.5.3
- 0.5.2
- 0.5.1
- 0.5.0
- 0.4.1
- 0.4.0
- 0.3.7
- 0.3.6
- 0.3.5
- 0.3.4
- 0.3.3
- 0.3.2
- 0.3.1
- 0.3.0
- 0.1.12
- 0.1.11
- 0.1.10
- 0.1.9
- 0.1.8
- 0.1.7
- 0.1.6
- 0.1.5
- 0.1.4
- 0.1.3
- 0.1.2
- 0.1.1
- 0.1.0
This package is auto-updated.
Last update: 2024-09-17 03:51:47 UTC
README
使用 Middleware (例如 Zend Expressive) 或 ZF2 的 MVC 框架的基本响应库
使其能够轻松快速地创建通用的 API 数据格式。
包含一个客户端 JavaScript (Angular.JS) 库。
支持多种常见的错误消息类型
- 数组
- 异常
- Http 状态码
- 输入过滤器 (Zend)
- 字符串
- 也可以注入自定义类型
示例:Middleware (例如 Zend Expressive)
// From a Middleware that extends Reliv\RcmApiLib\Middleware\AbstractJsonController /** EXAMPLE: InputFilter (Zend) **/ public function __invoke( ServerRequestInterface $request, ResponseInterface $response, callable $next ) { $inputFilter = new RcmGoogleAnalyticsFilter(); $inputFilter->setData($data); if (!$inputFilter->isValid()) { return $this->getApiResponse( [], 400, $inputFilter ); } }
返回类似的内容
{ "data": [], "messages": [ { "type": "inputFilter", "source": "validation", "code": "error", "value": "Some information was missing or invalid on the form. Please check the form and try again.", "primary": true, "params": [], "key": "inputFilter.validation.error" }, { "type": "validatorMessage", "source": "my-value", "code": "isEmpty", "value": "Value is required and can't be empty", "primary": null, "params": [], "key": "validatorMessage.my-value.isEmpty" } ] }
/** EXAMPLE: General **/ public function __invoke( ServerRequestInterface $request, ResponseInterface $response, callable $next ) { return $this->getApiResponse( ['my' => 'response'], 400, new ApiMessage( 'my-type', 'my-message-value {my-param}', 'my-source', 'my-code', true, ['my-param' => 'my-value'] ) ); }
示例:Zend 框架
// From a ZF2 Controller that extends \Reliv\RcmApiLib\Controller\AbstractRestfulJsonController // @see \Reliv\RcmApiLib\Controller\ExampleRestfulJsonController // Add exception message $this->addApiMessage( new \Exception('Some exception') ); // Add generic message as array $this->addApiMessage( [ 'key' => 'ArrayError', 'value' => 'Some {param} Message', 'primary' => true, 'type' => 'Array', 'code' => 'mycode', 'params' => ['param' => 'array message'] ] ); // Add generic message as object $this->addApiMessage( new ApiMessage('MYKEY', 'Some Message') ); // Add HTTP sttus message $this->addApiMessage( new HttpStatusCodeApiMessage(403) ); // Add inputFilter message $inputFilter = new \Zend\InputFilter\InputFilter(); // Use you own inputFilter here $this->addApiMessage( $inputFilter ); // Return the response return $this->getApiResponse( null, $statusCode = 200, $inputFilter, true ); // Return the response with your data and no messages return $this->getApiResponse( ['myThing' => 'someThing'], );
作者
James Jervis jjervis@relivinc.com 版权所有 (c) 2015, Reliv' International, Inc. https://github.com/reliv/rcm-api-lib