egeatech/laravel-responses
Composer 安装命令:
composer require egeatech/laravel-responses
包简介
A simple package to better handle application responses.
关键字:
README 文档
README
A package to provide some standardized response classes.
Installation
This package supports Laravel 6, 7, 8 and 9 but requires at least PHP 8.0. PHP 7.4 is supported up to version 2.1.1.
Via Composer
$ composer require egeatech/laravel-responses
Usage
Simply return a new instance of either ApiResponse or PaginatedApiResponse from your controller method.
Both classes share a similar signature for their constructors:
Here is the constructor for ApiResponse:
public function __construct( # Either null, a valid Eloquent model or an \Illuminate\Support\Collection instance or a generic payload mixed $responseData, # A classFQN extending Laravel JsonResource instance, to be used to know how to map response data. Can be null if data don't need formatting. ?string $responseFormatter, # A valid HTTP status code, to be returned to the caller int $httpStatus = \Illuminate\Http\JsonResponse::HTTP_OK, # An optional ApplicationException instance, to properly provide a valid error message representation ?\EgeaTech\LaravelExceptions\Interfaces\Exceptions\LogicErrorException $logicException = null ) {}
And here the one for PaginatedApiResponse class:
public function __construct( # A standard Laravel paginator instance, holding both data and pagination information \Illuminate\Contracts\Pagination\LengthAwarePaginator $paginatorData, # A classFQN extending Laravel JsonResource instance, to be used to know how to map response data string $responseFormatter, # A valid HTTP status code, to be returned to the caller int $httpStatus = \Illuminate\Http\JsonResponse::HTTP_OK, # An optional ApplicationException instance, to properly provide a valid error message representation ?\EgeaTech\LaravelExceptions\Interfaces\Exceptions\LogicErrorException $logicException = null ) {}
An example usage for the ApiResponse class could be the following:
<?php namespace App\Http\Controllers\Api\TestController; use App\Http\Controllers\Controller; use App\Http\Requests\ModelUpdateRequest; use EgeaTech\LaravelResponses\Http\Responses\ApiResponse; use EgeaTech\LaravelExceptions\Interfaces\Exceptions\LogicErrorException; class TestController extends Controller { public function __invoke(ModelUpdateRequest $request): ApiResponse { $occurredException = null; $databaseModel = null; try { $modelData = $request->validated(); $databaseModel = $this->updateModel($modelData); } catch (LogicErrorException $exception) { $occurredException = $exception; } return new ApiResponse( $databaseModel, DatabaseModelResource::class, $occurredException ? $occurredException->getCode() : ApiResponse::HTTP_ACCEPTED, $occurredException ); } }
For the other class, its usage is the same as long as you provide a LenghtAwarePaginator instance as first parameter.
Change log
Please see the CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email author email instead of using the issue tracker.
Credits
License
The software is licensed under MIT. Please see the LICENSE file for more information.
egeatech/laravel-responses 适用场景与选型建议
egeatech/laravel-responses 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.02k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 05 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「responses」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 egeatech/laravel-responses 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 egeatech/laravel-responses 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 egeatech/laravel-responses 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Simple helpers for Symfony to standardize the Api Responses and input validators
A set of helper classes to segregate the transformation logic from controller to separate dedicated classes.
Laravel package for formatted API responses.
A Package For Easy And Structured Api Response
Alfabank REST API integration
统计信息
- 总下载量: 4.02k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-05-16