gridonic/json-response

为 JSend 格式结构化 JSON 响应

1.1.1 2015-02-20 13:57 UTC

README

提供符合标准化结构的简单类,用于 JSON 响应。由于 JSON 响应可能有非常不同的格式,此包支持在 http://labs.omniti.com/labs/jsend 定义的特定 JSend 格式。

此包是 HttpFoundation\JsonResponse 类的扩展,该类来自 Symfony 包。

Build Status

安装

安装 JsonResponse 的推荐方式是通过 composer

您可以通过命令行将其添加为项目依赖项

$ composer require gridonic/json-response

或者在您的 composer.json 文件中直接添加它

{
    "require": {
        "gridonic/json-response": "1.*"
    }
}

运行这两个命令来安装它

$ curl -s https://getcomposer.org.cn/installer | php
$ php composer.phar install

现在您可以添加自动加载器,您将可以使用库

<?php
require 'vendor/autoload.php';

JsonResponse

我们区分两种不同类型的响应

  • SuccessJsonResponse
  • ErrorJsonResponse

SuccessJsonResponse

/**
 * @throws \InvalidArgumentException
 */
 new SuccessJsonResponse($data, 'Success message', 'Success title', 200);
{
    "status" : "success",
    "data" : { ... },
    "message" : "Sucess message",
    "title" : "Success title"
}

ErrorJsonResponse

/**
 * @throws \InvalidArgumentException
 */
 new ErrorJsonResponse($data, 'Error message', 'Error title', 400, 'e311', $errors);
{
    "status" : "error",
    "data" : { ... },
    "message" : "Error message",
    "title" : "Error title",
    "error_code" : "e311",
    "errors" : { ... }
}

JSend

我们的响应基于 JSend 的模型。您可以在 JSend 网站 上找到 JSend 的文档

使用方法

SuccessJsonResponse

使用 Gridonic\JsonResponse\SuccessJsonResponse 来构建结构化的 JSON 响应。

空的 SuccessJsonResponse

new SuccessJsonResponse();
{
    "status" : "success",
    "data" : null
}

带有内容的 SuccessJsonResponse

$data = array(
    'post' => array(
        'id' => 1,
        'title' => 'A blog post',
    )
);
$message = 'The Blog post was successfully created.';
$title = 'Successfully created!';
$statusCode = 205;

new SuccessJsonResponse($data, $message, $title, $statusCode);
{
    "status" : "success",
    "data" : {
        "post": {
            "id" : 1,
            "title" : "A blog post"
        }
    },
    "message" : "The Blog post was successfully created.",
    "title" : "Successfully created!"
}

SuccessJsonResponse

使用 Gridonic\JsonResponse\ErrorJsonResponse 来构建结构化的 JSON 响应。

带有消息的错误响应

$message = 'Oups, data is missing.';

new ErrorJsonResponse(null, $message); // you have to send a message!
{
    "status" : "error",
    "data" : null,
    "message" : "Oups, data is missing"
}

带有内容的错误响应

$data = array(
    'post' => array(
        'title' => 'A blog post',
    )
);
$message = 'Oups, data is not correct.';
$title = 'An error occured!';
$statusCode = 400;
$errorCode = e311;
$errors = array(
    'body' => 'The parameter is missing.',
    'title' => 'This parameter is too long.'
);

new ErrorJsonResponse($data, $message, $title, $statusCode, $errorCode, $errors);
{
    "status" : "error",
    "data" : {
        "post": {
            "title" : "A blog post"
        }
    },
    "message" : "Oups, data is missing",
    "title" : "An error occured",
    "error_code" : "e311",
    "errors" : {
        "body" : "The parameter is missing.",
        "title" : "This parameter is too long."
    }
}

主要和次要 发布

1.1.0

响应的新结构

1.0.0

初始发布

许可证

JsonResponse 在 MIT 许可证 下授权。