milito/response-generator

我使用这个包来简化修改JSON响应结构的操作。

v1.0.0 2022-10-14 19:22 UTC

This package is auto-updated.

Last update: 2024-09-15 00:06:27 UTC


README

Latest Version on Packagist Total Downloads License

响应生成器

Laravel项目的JSON响应生成器

我使用这个包来简化更改JSON响应结构的操作。

简介

使用此包以获得更灵活的响应。此包只能在Laravel中使用。

用法

首先通过以下命令使用composer安装此包:

composer requires milito/response-generator

然后将MilitoResponseGeneratorServiceProvider服务提供者导入到config/app.php文件中

<?php

return [
    //....
    
    "providers" => [
        //...
                \Milito\ResponseGenerator\Providers\MilitoResponseGeneratorServiceProvider::class,
        //...
    ]
];

现在您可以使用MilitoResponseGenerator外观来生成您的响应。

  • MilitoResponseGenerator::success() : SuccessState 这用于成功响应。
  • MilitoResponseGenerator::failed() : FailedState 这用于失败响应。

示例

成功响应

这是成功响应的例子

<?php
use Illuminate\Support\Facades\Route;
use Milito\ResponseGenerator\Facades\MilitoResponseGenerator;

Route::get('/', function () {
    return MilitoResponseGenerator::success()
            ->succeeded() // Sets the `Http Status code` field of response to `HTTP_OK` (200).
            ->message("Success response message") // Sets the `message` field of response.
            ->data([
                "body" => "Success response body"
            ]) // Sets the `data` field of response.
            ->send(); // Generates the response as a `json` response.
});

响应应该是这样的

{
  "message": "Success response message",
  "success": true,
  "code": 200,
  "data": {
    "body": "Success response body"
  }
}
失败响应

这是失败响应的例子

<?php
use Illuminate\Support\Facades\Route;
use Milito\ResponseGenerator\Facades\MilitoResponseGenerator;

Route::get('/', function () {
    return MilitoResponseGenerator::failed()
            ->code(\Illuminate\Http\Response::HTTP_INTERNAL_SERVER_ERROR) // Sets the `Http Status code` field of response to `SERVER_ERROR` (500).
            ->message("Error Response message") // Sets the `message` field of response.
            ->errors(["Yeah this is error"]) // Sets the `error` & the `errors` fields of response.
            ->send(); // Generates the response as a `json` response.
});

响应应该是这样的

{
  "message": "Error Response message",
  "success": false,
  "code": 500,
  "error": "Yeah this is error",
  "errors": {
    "all": [
      "Yeah this is error"
    ]
  }
}

注意1:如果您不想显示successcodeerrors字段,您可以通过配置文件来禁用它们。

注意2:如果您想更改字段的位置,您可以通过配置文件来更改。

文档

函数 & 类

现在,我们来了解如何处理成功响应。例如,我们使用外观来启动响应,逐步设置响应字段。现在我们需要了解如何使用这些函数以及如何生成我们的成功响应。

成功响应

SuccessState

使用MilitoResponseGenerator::success()函数启动成功响应后,我们将有一个SuccessState类型的对象。我们将为SuccessState(这是一个对象)有以下函数

注意:如果使用了updated()函数,这意味着我们需要无内容响应。因此,下一步是SendState以生成响应。

MessageState

MessageState有以下函数

DataState

DataState有以下函数

注意1:输入类型为mixed,因为我们把arrayobjectjson resources等传递给我们的数据函数。

注意2:我们可以发送一个空数据响应。

SendState

SendState是最后一步。在此之后,我们使用send函数生成响应并将其返回给客户端。

失败响应

FailedState

使用MilitoResponseGenerator::failed()函数启动失败响应后,我们将有一个FailedState类型的对象。我们将为FailedState(这是一个对象)有以下函数

MessageState

MessageState有以下函数

MessageState

MessageState有以下函数

注意1:输入类型为mixed,因为我们把arrayobjectstring等传递给我们的错误函数。

注意2:我们可以生成不包含errorerrors字段值的响应。

DataState

DataState有以下函数

注意1:输入类型为mixed,因为我们把arrayobjectjson resources等传递给我们的数据函数。

注意2:我们可以发送一个空数据响应。

注意3:有时我们可能在请求失败后发送数据。因此,这里提供了DataState

SendState

SendState是最后一步。在此之后,我们使用send函数生成响应并将其返回给客户端。

发布配置

使用以下命令发布配置文件以更改响应字段名称或使某些字段在响应中不可见

php artisan vendor:publish --tag=milito-response-config

测试

使用以下命令来运行测试

composer test

许可

MIT