nnjeim/respond
Composer 安装命令:
composer require nnjeim/respond
包简介
Laravel response helpers
README 文档
README
A Laravel response helper methods. The package respond provides a fluent syntax to form array or json responses.
In its configuration file, it allows the addition of custom methods.
Installation
You can install the package via composer:
composer require nnjeim/respond
Configuration
php artisan vendor:publish --provider="Nnjeim\Respond\RespondServiceProvider"
The respond.php config file allows:
- The presetting of the response format array/json.
- The possibility to edit the response parameters.
- The possibility to add custom methods.
Usage
Respond Facade
use Nnjeim\Fetch\Fetch;
use Nnjeim\Respond\Respond;
['response' => $response, 'status' => $status] = Fetch::setBaseUri('https://someapi.com/')->get('countries');
if ($status === 200 && $response->success) {
return Respond::toJson()
->setMessage('countries')
->setData($response->data)
->withSuccess();
}
abort(400);
RespondHelper Instantiation
use Nnjeim\Respond\RespondHelper;
private RespondHelper $respond;
private array $data;
private bool $success;
public function __construct(RespondHelper $respond)
{
$this->respond = $respond;
}
.
.
.
$respond = $this
->respond
->toJson()
->setMessage('countries')
->setData($data);
if ($this->success)
{
return $respond->withSuccess()
}
return $respond->withErrors();
Methods
Set the status code
Sets the response status code
@return $this setStatusCode(int $statusCode)
Set the message
Sets the response title message
@return $this setMessage(string $message)
Set the meta data
Sets the response meta data. The meta data will be merged with the response data array.
@return $this setMeta(array $meta)
Set the data
Sets the response data array.
@return $this setData(array $data)
Set the errors
Sets the response errors.
@return $this setErrors(array $errors)
respond in Json format
returns an instance of Illuminate\Http\JsonResponse
this method overwrites the config `toJson` set value.
@return $this toJson()
Respond with success
On success response. The default response status code is 200.
@return array|JsonResponse withSuccess()
Respond with created
On created response. The default response status code is 201.
@return array|JsonResponse withCreated()
Respond with accepted
On accepted response. The default response status code is 202.
@return array|JsonResponse withAccepted()
Respond with no content
On success response with no results found. The default status code is 204
@return array|JsonResponse withNoContent()
Respond with errors
On error response. The default response status code is 422.
@return array|JsonResponse withErrors(?array $errors = null)
Respond with server error
On server error response. The default response status code is 500.
@return array|JsonResponse withServerError()
Respond with not found
Record not found error. The default response status code is 404.
@return array|JsonResponse withNotFound()
Respond with not authenticated
Not authenticated reponse. The default response status code is 401.
@return array|JsonResponse withNotAuthenticated()
Respond with not authorized
Not authorized reponse. The default response status code is 403.
@return array|JsonResponse withNotAuthorized()
Respond
@return array|JsonResponse
[
'response' => [
'success' => true,
'message' => 'message',
'data' => [],
'errors' => [],
],
'status' => 200,
];
Custom methods
In the respond.php config file, in the responses array add an array entry where the key is name of the method in lower
case and the value contains the desired success, message and status params.
//example
'methodnotallowed' => [
'success' => false,
'message' => 'the method not allowed!',
'status' => Response::HTTP_METHOD_NOT_ALLOWED,
],
Usage
Respond::withMethodNotAllowed();
Testing
The helpers and methods are tested with 99% coverage.
To run the tests.
composer install
composer test
To run the coverage test.
composer test-coverage
Changelog
Please see CHANGELOG for more information what has changed recently.
nnjeim/respond 适用场景与选型建议
nnjeim/respond 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 41 次下载、GitHub Stars 达 5, 最近一次更新时间为 2021 年 09 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「response」 「laravel」 「responder」 「lumen」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 nnjeim/respond 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nnjeim/respond 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 nnjeim/respond 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel package for building API responses
HTTP request logger middleware for Laravel
Class to generate a standard structure for api json responses
A api responder
Alfabank REST API integration
Speed up a Laravel application by caching the entire response additions
统计信息
- 总下载量: 41
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-09-12