nnjeim / respond
Laravel 响应助手
1.2.1
2021-09-20 05:37 UTC
Requires
- php: >=7.4
Requires (Dev)
- orchestra/testbench: >=v4.0.0
- phpunit/phpunit: >=8.5.8
This package is auto-updated.
Last update: 2024-09-05 19:19:53 UTC
README
一个 Laravel 响应助手方法。包 respond 提供了一种流畅的语法来形成数组或 JSON 响应。
在其配置文件中,它允许添加自定义方法。
安装
您可以通过 composer 安装此包
composer require nnjeim/respond
配置
php artisan vendor:publish --provider="Nnjeim\Respond\RespondServiceProvider"
配置文件 respond.php 允许
- 预设置响应格式为 array/json。
- 编辑响应参数的可能性。
- 添加自定义方法的可能性。
使用方法
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 实例化
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();
方法
设置状态码
Sets the response status code
@return $this setStatusCode(int $statusCode)
设置消息
Sets the response title message
@return $this setMessage(string $message)
设置元数据
Sets the response meta data. The meta data will be merged with the response data array.
@return $this setMeta(array $meta)
设置数据
Sets the response data array.
@return $this setData(array $data)
设置错误
Sets the response errors.
@return $this setErrors(array $errors)
以 JSON 格式响应
returns an instance of Illuminate\Http\JsonResponse
this method overwrites the config `toJson` set value.
@return $this toJson()
成功响应
On success response. The default response status code is 200.
@return array|JsonResponse withSuccess()
创建响应
On created response. The default response status code is 201.
@return array|JsonResponse withCreated()
接受响应
On accepted response. The default response status code is 202.
@return array|JsonResponse withAccepted()
无内容响应
On success response with no results found. The default status code is 204
@return array|JsonResponse withNoContent()
错误响应
On error response. The default response status code is 422.
@return array|JsonResponse withErrors(?array $errors = null)
服务器错误响应
On server error response. The default response status code is 500.
@return array|JsonResponse withServerError()
未找到响应
Record not found error. The default response status code is 404.
@return array|JsonResponse withNotFound()
未认证响应
Not authenticated reponse. The default response status code is 401.
@return array|JsonResponse withNotAuthenticated()
未授权响应
Not authorized reponse. The default response status code is 403.
@return array|JsonResponse withNotAuthorized()
响应
@return array|JsonResponse
[
'response' => [
'success' => true,
'message' => 'message',
'data' => [],
'errors' => [],
],
'status' => 200,
];
自定义方法
在配置文件 respond.php 中,在响应数组中添加一个数组条目,键为方法名的小写,值为所需的成功、消息和状态参数。
//example
'methodnotallowed' => [
'success' => false,
'message' => 'the method not allowed!',
'status' => Response::HTTP_METHOD_NOT_ALLOWED,
],
使用方法
Respond::withMethodNotAllowed();
测试
助手和方法通过 99% 的覆盖率进行测试。
运行测试。
composer install
composer test
运行覆盖率测试。
composer test-coverage
变更日志
请参阅 CHANGELOG 获取更多关于最近更改的信息。