承接 pmarien/rest-api-response 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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

Build Status Coverage Status SensioLabsInsight

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.

统计信息

  • 总下载量: 142
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-10-30

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固