pmarien/rest-api-response
最新稳定版本:2.0.0
Composer 安装命令:
composer require pmarien/rest-api-response
包简介
This library provides a extension for the symfony/http-foundation component to build standardized json responses.
README 文档
README
This Library provides a Response abstraction for RESTful JSON-API's on top of the Symfony HTTP-Foundation-Component.
The Library defines custom Exceptions and Exception-Interfaces for Error Handling.
There is a Response-Class for handling of successfull responses and an Error-Response-Class for Exception-Responses.
Install with Composer
composer require pmarien/rest-api-response
Version
If you use the symfony/http-foundation in any version lower than 3.0 please use version 1.* of these library.
If you use the symfony/http-foundation in any version higher than 3.0 please use version 2.* of these library.
Usage
JSON Output
Status:
indicates, weather the request was successful or not
Count:
Number of results which are delivered within the results array
Results:
Array of one or more data objects (your data)
Success
{
"status": "success",
"count": 1,
"results": [
{
"custom": "data"
}
]
}
Error
{
"status": "error",
"count": 1,
"results": [
{
"code": 0,
"message": "Error Message",
"previous": null,
"data": [
"optional custom debug data, can be an object or an array"
]
}
]
}
Get a Response Object
$response = new ApiResponse(['message'=>'Hello World']);
$errorResponse = new ApiResponse(new \Exception('Request failed!'));
Object Handling
By default, protected and private properties of objects will be ignored be json_encode().
To build a response from custom objects, this library provides an Interface (JsonEncodableInterface). Objects which implement these Interface can be handled on custom way.
Example
your Class
class CustomObject implements JsonEncodableInterface {
/**
* @var string
*/
protected $publicForResponse = 'hello';
/**
* @var string
*/
protected $notPublic = 'world';
/**
* Return an array of properties which can be encoded as json
* @return array
*/
public function encode(){
return [
'public' => $this->publicForResponse
];
}
}
your Controller Action
public function testAction(){
return new ApiResponse(new CustomObject());
}
Json-Response
{
"status": "success",
"count": 1,
"results": [
{
"public": "hello"
}
]
}
Exceptions
There are four special Exceptions and Interfaces defined in this Library:
ExceptionInterface:
Provides a Method called "getMetaData" which should return an array with custom debug data for the error response
UncriticalExceptionInterface:
If an Exception implements these Interface, the error response will return the error object and status but with a HTTP-Status-Code 200 (Ok)
CriticalExceptionInterface:
With these Interface you are able to define a custom HTTP-Status-Code instead of the default 500 (Internal Server Error) Status
ExceptionListInterface:
With these Interface you are able to define more than one error result object for one response
Please note that your custom Exception must extend the basic php exception, even if you implement one ore more of the interfaces.
There are also predefined exceptions ready to use:
UncriticalException:
Implements the ExceptionInterface and the UncriticalExceptionInterface
CriticalException:
Implements the ExceptionInterface and the CriticalExceptionInterface
UncriticalExceptionExceptionList:
Implements the ExceptionInterface, the UncriticalExceptionInterface and the ExceptionListInterface
CriticalExceptionExceptionList:
Implements the ExceptionInterface, the CriticalExceptionInterface and the ExceptionListInterface
Licence
This library is under MIT Licence.
pmarien/rest-api-response 适用场景与选型建议
pmarien/rest-api-response 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 142 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 10 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 pmarien/rest-api-response 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 pmarien/rest-api-response 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 142
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-10-30