rigsto/api-http-status
Composer 安装命令:
composer require rigsto/api-http-status
包简介
HTTP status codes and resources for API usages
README 文档
README
A simple enum class for HTTP status codes and their associated API response.
All codes are taken from Wikipedia.
Installation
Using composer:
$ composer require rigsto/api-http-status
Usage
Http Status Code
Enum
<?php use Rigsto\ApiHttpStatus\HttpStatus; HttpStatus::OK; HttpStatus::NOT_FOUND; HttpStatus::INTERNAL_SERVER_ERROR; HttpStatus::BAD_REQUEST; HttpStatus::UNAUTHORIZED;
Functions
getStatusCode() will return the status code.
getName() will return the http name.
getCategory() will return the http category.
isSuccess() will return true if the status code is a success code.
<?php use Rigsto\ApiHttpStatus\HttpStatus; $http = HttpStatus::OK; $http->getStatusCode(); // 200 $http->getName(); // Ok $http->getCategory(); // Success $http->isSuccess(); // true $http = HttpStatus::UNAUTHORIZED; $http->getStatusCode(); // 401 $http->getName(); // Unauthorized $http->getCategory(); // Client Error $http->isSuccess(); // false $http = HttpStatus::INTERNAL_SERVER_ERROR; $http->getStatusCode(); // 500 $http->getName(); // Internal Server Error $http->getCategory(); // Server Error $http->isSuccess(); // false
Generate Http Status From Code
isValidCode() will return true if the code is valid and false if it is not.
fromCode() will return an HttpStatus object if the code is valid and null if it is not.
<?php use Rigsto\ApiHttpStatus\HttpStatus; $code = 200; $codeValidity = HttpStatus::isValidCode($code); // true $http = HttpStatus::fromCode($code); // HttpStatus::OK $code = 999 $codeValidity = HttpStatus::isValidCode($code); // false $http = HttpStatus::fromCode($code); // null
Api Response
generateResponse(HttpStatus, ?message, ?data) will return json string of the http status, message, and data.
HttpStatus is required, but message and data are optional. If message is null, then message will be generated from http status name.
<?php use Rigsto\ApiHttpStatus\HttpStatus; use Rigsto\ApiHttpStatus\ApiResponse; $data = [...]; $response = ApiResponse::generateResponse(HttpStatus::OK, null, $data); // {"success": true, "code": 200, "message": "Ok", "data": [...]} $response = ApiResponse::generateResponse(HttpStatus::BAD_REQUEST, 'Custom Message', $data); // {"success": false, "code": 400, "message": "Custom Message", "data": [...]} $response = ApiResponse::generateResponse(HttpStatus::INTERNAL_SERVER_ERROR, null, null); // {"success": false, "code": 500, "message": "Internal Server Error", "data": null}
generateSuccessResponse(?message, ?data) will return json string of the http status, message, and data. This method is same with function above, but it generates response with HttpStatus::OK as http status.
<?php use Rigsto\ApiHttpStatus\ApiResources; $data = [...]; $response = ApiResources::generateSuccessResponse(); // {"success": true, "code": 200, "message": "Ok", "data": null} $response = ApiResources::generateSuccessResponse(message: 'Custom Message'); // {"success": true, "code": 200, "message": "Custom Message", "data": null} $response = ApiResources::generateSuccessResponse(data: $data); // {"success": true, "code": 200, "message": "Ok", "data": [...]}
generateUnauthorizedResponse() will return json string with unauthorized http status and message.
<?php use Rigsto\ApiHttpStatus\ApiResources; $response = ApiResources::generateUnauthorizedResponse(); // {"success": false, "code": 401, "message": "Unauthorized", "data": null}
generatePaginationResponse(HttpStatus, ?message, ?data will return json string of http status, message, and paginate data.
Function concept same as generateResponse() but with pagination data.
<?php use Rigsto\ApiHttpStatus\HttpStatus; use Rigsto\ApiHttpStatus\ApiResponse; $data = ["your data here" => "..."]; $response = ApiResponse::generatePaginationResponse(HttpStatus::OK, null, $data);
{
"success": true,
"code": 200,
"message": "Ok",
"data": {
"data": {
"your data here":"..."
},
"pagination": {
"total": 50,
"per_page": 10,
"current_page": 1,
"last_page": 5,
"from": 1,
"to": 10
}
}
}
rigsto/api-http-status 适用场景与选型建议
rigsto/api-http-status 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 73 次下载、GitHub Stars 达 1, 最近一次更新时间为 2022 年 04 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 rigsto/api-http-status 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rigsto/api-http-status 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 73
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-04-18