cerenimo / laravel-responses
Laravel 的标准化响应。
v1.2.4
2024-06-26 19:47 UTC
Requires
README
composer require cerenimo/laravel-responses
ResponseTrait 是一个用于在 Laravel 和 Lumen 中标准化 HTTP 响应消息的 trait。您可以使用上述 composer 命令安装它。要在您的类中使用它,请使用 "use" 关键字添加以下行
use Cerenimo\LaravelResponses\Response; class YourClass { use Response; // ... }
在您的函数内部,您可以如下使用它
return $this->setMessage('successfully') ->responseSucces();
响应 / 200 OK
{ "result": true, "message": "successfully" }
参数
- setCustomData($key,$value)
- setMessage($message)
- setStatusCode($statusCode)
- setValidation($validation)
- setException($exception)
- setPagination($pagination)
- setDataName($dataName)
该软件包提供以下函数
responseSuccess()
-
如果请求成功则使用。
-
参数可选。可以添加 setCustomData, setMessage 和 setStatusCode。
-
使用示例
$this->setCustomData('user', $user) ->setCustomData('token', $token) ->setMessage('User Created') ->setStatusCode(201) ->responseSuccess();
$this->responseSuccess();
$this->setMessage('Successfully!')->responseSuccess();
-
响应示例
{ "result": true, "message": "User Created", "data": { "user": { "id": 372, "name": "Ceren Özkurt" }, "token" : "Bfafa98a7f8afkafafaf98afajfka" } }
-
responseDataWithPagination()
- 如果服务器不支持满足请求所需的功能则使用。
-
应添加 setPagination,并可以添加 setMessage 和 setDataName。
-
使用示例
$this->setPagination($setPagination) ->setDataName('files') ->responseDataWithPagination();
-
响应示例
{ "result": true, "data": { "files": [ { "id": 60, "userId": "auth0|64a7fd7bc8e7f423cb5285b2", "filename": "seller-288138-barkod-bazlı-iade-raporu-2023.05.03-18.19.04.xlsx", "fullPathName": "/home/rontest/public_html/uploads/64a7fd7bc8e7f423cb5285b2/1688731195_jaTuvvO7dSrFzcJy.xlsx", "type": "excel", "createdAt": "2023-07-07T11:59:55.000000Z", "updatedAt": "2023-07-07T11:59:55.000000Z" }, { "id": 59, "userId": "auth0|64a7fd7bc8e7f423cb5285b2", "filename": "Ağrı Dağı Efsanesi .pdf", "fullPathName": "/home/rontest/public_html/uploads/64a7fd7bc8e7f423cb5285b2/1688731130_74bI3pGXa9yq7Fda.pdf", "type": "pdf", "createdAt": "2023-07-07T11:58:50.000000Z", "updatedAt": "2023-07-07T11:58:50.000000Z" } ] }, "page": { "links": { "first": true, "last": true, "prev": null, "next": null }, "meta": { "current_page": 1, "from": 1, "to": 2, "per_page": 25, "total": 2 } } }
-
responseError()
- 如果请求出现错误则使用。
-
参数可选。可以添加 setMessage 和 setStatusCode。
-
使用示例
$this->setMessage('Failed') ->setStatusCode(400) ->responseError();
$this->responseError(); //default message and status code
$this->setMessage('Failed!') ->responseSuccess();
-
响应示例
{ "result": false, "message": "Failed", }
-
responseValidation()
- 如果存在验证错误则使用。
-
参数必需。应添加 setValidation。
-
使用示例
$this->setValidation($validation) ->responseValidation();
-
响应示例
{ "result": false, "validation_error": { "name": [ "The name field is required." ] } }
-
responseNotFound()
- 如果出现未找到错误则使用。
-
参数可选。可以添加 setMessage。
-
使用示例
$this->setMessage('Failed') ->responseNotFound();
$this->responseNotFound();
-
响应示例
{ "result": false, "error": "Failed" }
-
responseForbidden()
- 用于禁止错误。
-
参数可选。可以添加 setMessage。
-
使用示例
$this->setMessage('No Permission') ->responseForbidden();
$this->responseForbidden();
-
响应示例
{ "result": false, "error": "No access permission." }
-
responseUnauthorized()
- 用于未授权错误。
-
参数可选。可以添加 setMessage。
-
使用示例
$this->setMessage('No Authorized') ->responseUnauthorized();
$this->responseUnauthorized();
-
响应示例
{ "result": false, "error": "Not authorized." }
-
responseTryCatch()
- 用于 try-catch 错误。
-
*参数必需。应添加 setException,并可添加 setStatusCode.*
-
使用示例
$this->setException($e) ->responseTryCatch();
-
响应示例
{ "result": false, "error": "sql connection error" }
-
参数可选。可以添加 setMessage。
-
使用示例
$this->setMessage('Failed') ->responseValidation();
$this->responseValidation();
-
响应示例
{ "result": false, "error": "Failed" }
-
responseBadRequest()
- 如果发送的请求不正确则使用。
-
参数可选。可以添加 setMessage。
-
使用示例
$this->setMessage('Failed') ->responseBadRequest();
$this->responseBadRequest();
-
响应示例
{ "result": false, "error": "Bad request." }
-
responseConflict()
- 如果由于预定规则或请求的 web 服务器上的版本差异导致不匹配,则使用。
-
参数可选。可以添加 setMessage。
-
使用示例
$this->setMessage('Failed') ->responseConflict();
$this->responseConflict();
-
响应示例
{ "result": false, "error": "Conflict." }
-
responsePayloadTooLarge()
- 如果请求实体比服务器定义的限制大得多,则使用。
-
参数可选。可以添加 setMessage。
-
使用示例
$this->setMessage('Failed') ->responsePayloadTooLarge();
$this->responsePayloadTooLarge();
-
响应示例
{ "result": false, "error": 'Payload too large.' }
-
responseTooManyRequests()
- 如果网站超过了指定的请求限制,则使用。
-
参数可选。可以添加 setMessage。
-
使用示例
$this->setMessage('Failed') ->responseTooManyRequests();
$this->responseTooManyRequests();
-
响应示例
{ "result": false, "error": "Too many requests." }
-
responseInternalServer()
- 如果发生服务器端错误则使用。
-
参数可选。可以添加 setMessage。
-
使用示例
$this->setMessage('Failed') ->responseInternalServer();
$this->responseInternalServer();
-
响应示例
{ "result": false, "error": 'Internal server error.' }
-
responseNotImplemented()
- 如果服务器不支持满足请求所需的功能则使用。
-
参数可选。可以添加 setMessage。
-
使用示例
$this->setMessage('Failed') ->responseNotImplemented();
$this->responseNotImplemented();
-
响应示例
{ "result": false, "error": 'Not implemented.' }
-
responseInvalidToken()
- 如果令牌无效则使用。
-
参数可选。可以添加 setMessage。
-
使用示例
$this->setMessage('Failed') ->responseInvalidToken();
$this->responseInvalidToken();
-
响应示例
{ "result": false, "error": 'Invalid token.' }
-