milito/response-generator
Composer 安装命令:
composer require milito/response-generator
包简介
I use this package to make it easier to modify the structure of json responses.
README 文档
README
Response Generator
Json Response Generator for Laravel Projects
I use this package to make it easier to change the JSON response structure.
Introduction
Use this package to make more flexible responses. This package can only be used in Laravel.
Usage
First install the package via composer using the following command :
composer requires milito/response-generator
Then import the MilitoResponseGeneratorServiceProvider service provider to config/app.php file :
<?php return [ //.... "providers" => [ //... \Milito\ResponseGenerator\Providers\MilitoResponseGeneratorServiceProvider::class, //... ] ];
Now you can use MilitoResponseGenerator facade to generate your responses.
MilitoResponseGenerator::success() : SuccessStateThis is used for success responses.MilitoResponseGenerator::failed() : FailedStateThis is used for failed responses.
Example
Success Response
This is for success response :
<?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. });
The response should be like this :
{
"message": "Success response message",
"success": true,
"code": 200,
"data": {
"body": "Success response body"
}
}
Failed Response
This is for failed response :
<?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. });
The response should be like this :
{
"message": "Error Response message",
"success": false,
"code": 500,
"error": "Yeah this is error",
"errors": {
"all": [
"Yeah this is error"
]
}
}
Note 1 : If you don't want to show
success,code, orerrorsfields, you can disable them through the config file.
Note 2 : If you want to change the position of the fields, you can change it through the config file.
Docs
Functions & Classes
Now, how to handle success response. For example, we use facade to start a response, we set response fields step by step. Now we need to know how to work with these functions and how to generate our success response.
Success Response
SuccessState
After usingMilitoResponseGenerator::success() function to start a success response, we will have an object of SuccessState type.
We will have the following functions for SuccessState (This is an object) :
| Function | Input | Output | Description |
|---|---|---|---|
code(int $code) |
int status_code |
MessageState object |
Sets Http code (what ever you want) |
| succeeded() | void |
MessageState object |
Sets Http code as HTTP_OK code (200) |
| created() | void |
MessageState object |
Sets Http code as HTTP_CREATED code (201) |
| accepted() | void |
MessageState object |
Sets Http code as HTTP_ACCEPTED code (202) |
| updated() | void |
SendState object |
Sets http code as HTTP_NO_CONTENT code and doesn't need a body (204) |
Note : If used
updated()function, it means we needno contentresponse. Because of that, the next state isSendStateto generate the response.
MessageState
MessageState has these functions :
| Function | Input | Output | Description |
|---|---|---|---|
message(string $message) |
string message |
DataState object |
Sets the message field of response |
DataState
DataState has these functions :
| Function | Input | Output | Description |
|---|---|---|---|
data(mixed $data) |
mixed data |
SendState object |
Sets the data field of response |
send(array $headers = []) |
array headers |
JsonResponse object |
Generates Response. |
Note 1 :
mixedinput type because we passarrayorobjectorjson resourcesor etc... to our data function.
Note 2 : We can send a response with an empty data.
SendState
SendState is the last step. After that, we use send function to generate a response and return it to client.
| Function | Input | Output | Description |
|---|---|---|---|
send(array $headers = []) |
array headers |
JsonResponse object |
Generates Response. |
Failed Response
FailedState
After using MilitoResponseGenerator::failed() function to start a failed response, we will have an object of FailedState type.
We will have the following functions for FailedState (This is an object) :
| Function | Input | Output | Description |
|---|---|---|---|
code(int $code) |
int status_code |
MessageState object |
Sets the Http code (what ever you want) |
MessageState
MessageState has these functions :
| Function | Input | Output | Description |
|---|---|---|---|
message(string $message) |
string message |
ErrorState object |
Set message field of response |
MessageState
MessageState has these functions :
| Function | Input | Output | Description |
|---|---|---|---|
errors(mixed $errors) |
mixed message |
DataState object |
Sets the error & the errors (as object) fields of response |
send(array $headers = []) |
array headers |
JsonResponse object |
Generates Response. |
Note 1 :
mixedinput type because we passarrayorobjectorstringor etc... to our errors function.
Note 2 : We can generate response without the
error& theerrorsfields value.
DataState
DataState has these functions :
| Function | Input | Output | Description |
|---|---|---|---|
data(mixed $data) |
mixed data |
SendState object |
Sets the data field of response |
send(array $headers = []) |
array headers |
JsonResponse object |
Generates Response. |
Note 1 :
mixedinput type because we passarrayorobjectorjsonresources or etc... to our data function.
Note 2 : We can send a response with an empty data.
Note 3 : Sometimes we need to send data after our request failed. Because of that, we have
DataStatehere.
SendState
SendState is the last step. After that, we use send function to generate a response and return it to client.
| Function | Input | Output | Description |
|---|---|---|---|
send(array $headers = []) |
array headers |
JsonResponse object |
Generates Response. |
Publish config
Use the following command to publish the config file to change the response fields name or makes some fields hidden from the response :
php artisan vendor:publish --tag=milito-response-config
Test
Use the following command to run tests:
composer test
License
MIT
milito/response-generator 适用场景与选型建议
milito/response-generator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 34 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 10 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「json」 「response」 「laravel」 「structure」 「milito」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 milito/response-generator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 milito/response-generator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 milito/response-generator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Kinikit - PHP Application development framework MVC component
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
HTTP request logger middleware for Laravel
Class to generate a standard structure for api json responses
swagger-php - Generate interactive documentation for your RESTful API using phpdoc annotations
统计信息
- 总下载量: 34
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-10-14