signaturetech/laravel-api-response-builder
Composer 安装命令:
composer require signaturetech/laravel-api-response-builder
包简介
A super simple package allowing for consistent API responses throughout your Laravel application
README 文档
README
Laravel API Response Builder
Table of contents
Introduction
ResponseBuilder is a Laravel package, designed to help you build a nice, normalized and easy to consume
REST API JSON responses.
Installation & Configuration
You can install this package via composer using:
composer require signaturetech/laravel-api-response-builder
Next run the command below to setup api-response.config file, you can set your configuration.
php artisan vendor:publish --tag=response-builder
Examples
Example : Success
use SignatureTech\ResponseBuilder\ResponseBuilder;
public function index() {
$users = User::query()->take(2)->get();
return ResponseBuilder::success($users);
}
// Output
Status: 200 OK
{
"status": true,
"data": [
{
"id": 1,
"name": "Prof. Bell Hayes",
"email": "myundt@example.net",
"email_verified_at": "2022-12-07T05:27:30.000000Z",
"created_at": "2022-12-07T05:27:30.000000Z",
"updated_at": "2022-12-07T05:27:30.000000Z"
},
{
"id": 2,
"name": "Ms. Tessie Streich III",
"email": "dledner@example.net",
"email_verified_at": "2022-12-07T05:27:30.000000Z",
"created_at": "2022-12-07T05:27:30.000000Z",
"updated_at": "2022-12-07T05:27:30.000000Z"
}
]
}
Example : Custom Success
use SignatureTech\ResponseBuilder\ResponseBuilder;
public function login() {
$user = User::query()->first();
$token = Uuid::uuid4();
return ResponseBuilder::asSuccess()
->withMessage('Successfuly Login')
->with('auth_token', $token)
->withData([
'profile' => new UserResource($user)
])
->build();
}
// Output
Status: 200 OK
{
"status": true,
"message": "Successfuly Login",
"auth_token": "65050859-1a05-4fa8-827f-7023ff73d4a3",
"data": {
"profile": {
"id": 1,
"name": "Prof. Bell Hayes",
"email": "myundt@example.net"
}
}
}
Example : Error
use SignatureTech\ResponseBuilder\ResponseBuilder;
public function index() {
return ResponseBuilder::error('Sorry We are not able to getting your details', Response::HTTP_INTERNAL_SERVER_ERROR);
}
// Output
Status: 500 Internal Server Error
{
"status": false,
"message": "Sorry We are not able to getting your details"
}
Example : Custom Error
use SignatureTech\ResponseBuilder\ResponseBuilder;
public function index() {
return ResponseBuilder::asError(Response::HTTP_BAD_GATEWAY)
->withMessage('Sorry We are not able to getting your details')
->with('custom_key', 'value')
->withData([
'bad_gateway_error' => 'We are not able to process your request'
])
->build();
}
// Output
Status: 502 Bad Gateway
{
"status": false,
"message": "Sorry We are not able to getting your details",
"code": 10001,
"data": {
"bad_gateway_error": "We are not able to process your request"
}
}
Example : Validation Error Message
use SignatureTech\ResponseBuilder\Http\ValidationFailed;
class UserRequest extends FormRequest
{
use ValidationFailed;
.
.
}
// Output
Status: 400 Bad Request
{
"status": false,
"errors": {
"name": [
"The name field is required."
],
"email": [
"The email field is required."
]
}
}
- You can customize HttpStatus Code in
config/api-response.phpunder the variablevalidation_http_code. - You can customize your error message
config/api-response.phpunder the variableshow_validation_failed_message,allfor get all error messages andfirstfor get the first message.
Example:
// config/api-response.php
'show_validation_failed_message' => 'first',
// Output
Status: 400 Bad Request
{
"status": false,
"message": "The name field is required."
}
Example : Custom Filed
use SignatureTech\ResponseBuilder\ResponseBuilder;
public function index() {
$user = User::query()->first();
$token = Uuid::uuid4();
return ResponseBuilder::asSuccess()->with('auth_token', $token)->withData($user)->build();
}
// Output:
Status: 200 OK
{
"status": true,
"auth_token": "21d97007-e2b9-4ee1-86b1-3cfb96787436",
"data": {
"id": 1,
"name": "Prof. Bell Hayes",
"email": "myundt@example.net",
"email_verified_at": "2022-12-07T05:27:30.000000Z",
"created_at": "2022-12-07T05:27:30.000000Z",
"updated_at": "2022-12-07T05:27:30.000000Z"
}
}
Example : Pagination
use SignatureTech\ResponseBuilder\ResponseBuilder;
public function index() {
$user = User::query()->paginate(3);
return ResponseBuilder::asSuccess()->withPagination($user)->build();
}
// Output:
Status: 200 OK
{
"status": true,
"data": [
{
"id": 1,
"name": "Prof. Bell Hayes",
"email": "myundt@example.net",
"email_verified_at": "2022-12-07T05:27:30.000000Z",
"created_at": "2022-12-07T05:27:30.000000Z",
"updated_at": "2022-12-07T05:27:30.000000Z"
},
.
.
],
"meta": {
"total_page": 14,
"current_page": 1,
"total_item": 40,
"per_page": 3
},
"link": {
"next": true,
"prev": false
}
}
Example : Pagination and you are using JsonResource
use SignatureTech\ResponseBuilder\ResponseBuilder;
public function index() {
$user = User::query()->paginate(3);
return ResponseBuilder::asSuccess()->withPagination($user, UserResource::class)->build();
}
// Output:
Status: 200 OK
{
"status": true,
"data": [
{
"id": 1,
"name": "Prof. Bell Hayes",
"email": "myundt@example.net"
},
.
.
],
"meta": {
"total_page": 14,
"current_page": 1,
"total_item": 40,
"per_page": 3
},
"link": {
"next": true,
"prev": false
}
}
License
- Written and copyrighted ©2022 by Prem Chand Saini (prem@signaturetech.in)
- ResponseBuilder is open-sourced software licensed under the MIT license
signaturetech/laravel-api-response-builder 适用场景与选型建议
signaturetech/laravel-api-response-builder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 471 次下载、GitHub Stars 达 14, 最近一次更新时间为 2023 年 05 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 signaturetech/laravel-api-response-builder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 signaturetech/laravel-api-response-builder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 471
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 15
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-05-25