willywes/apiresponse
Composer 安装命令:
composer require willywes/apiresponse
包简介
Class to generate a standard structure for api json responses
README 文档
README
Class to generate a standard structure for api json responses.
Installation
Via Composer
$ composer require willywes/apiresponse
Usage
Imports
use Willywes\ApiResponse\ApiResponse;
Functions of Control (HTTP/200 OK)
Default functions that always return a http 200 code, but have a control state.
Params
| Param | Description |
|---|---|
| data | array of data for response (allow null) |
| message | custom message to response (optional) |
| title | custom title to response (optional) |
Functions
| Function | Description |
|---|---|
| JsonSuccess | Response status "success" with HTTP 200 |
| JsonError | Response status "error" with HTTP 200 |
| JsonWarning | Response status "warning" with HTTP 200 |
| JsonInfo | Response status "info" with HTTP 200 |
| JsonFieldValidation | Response status "field_validation" with HTTP 200 |
| JsonFieldValidationError | Response status "field_validation" with HTTP 422 |
Examples
Success example
//Execution in php return ApiResponse::JsonSuccess([ 'user' => User::first(), 'roles' => Role::all(), ]);
//Response { "status":"success", "title":"Operación Exitosa.", "message": null, "data":{ "user":{ "id":1, "full_name":"John Smith", "email":"jsmith@test.cl", "role_id":1 }, "roles":[ { "id":1, "name":"God Admin" }, { "id":2, "name":"Administrator" } ] } }
//HTTP Response Status Code: 200 OK
Error example
//Execution in php return ApiResponse::JsonError(null, 'something has gone wrong!', 'oops');
//Response { "status":"error", "title":"oops", "message":"something has gone wrong!", "data": null }
//HTTP Response Status Code: 200 OK
Functions with specific HTTP Code
Default functions that returns a specific http code, but in the same way the body responds
Params
| Param | Description |
|---|---|
| data | array of data for response (allow null) |
| message | custom message to response (optional) |
Functions Code HTTP 1XX
| Http Code | Function Name | Description |
|---|---|---|
| 100 | Http100 or Continue | Response status "info" with HTTP 100 |
| 101 | Http101 or SwitchingProtocols | Response status "info" with HTTP 101 |
| 102 | Http102 or Processing | Response status "info" with HTTP 102 |
| 103 | Http103 or EarlyHints | Response status "info" with HTTP 103 |
Functions Code HTTP 2XX
| Http Code | Function Name | Description |
|---|---|---|
| 200 | Http200 or Ok | Response status "success" with HTTP 200 |
| 201 | Http201 or Created | Response status "success" with HTTP 201 |
| 202 | Http202 or Accepted | Response status "success" with HTTP 202 |
| 203 | Http203 or NonAuthoritativeInformation | Response status "success" with HTTP 203 |
| 204 | Http204 or NoContent | Response status "success" with HTTP 204 |
| 205 | Http205 or ResetContent | Response status "success" with HTTP 205 |
| 206 | Http206 or PartialContent | Response status "success" with HTTP 206 |
Functions Code HTTP 3XX
| Http Code | Function Name | Description |
|---|---|---|
| 300 | Http300 or MultipleChoices | Response status "redirection" with HTTP 300 |
| 301 | Http301 or MovedPermanently | Response status "redirection" with HTTP 301 |
| 302 | Http302 or MovedTemporarily | Response status "redirection" with HTTP 302 |
| 303 | Http303 or SeeOther | Response status "redirection" with HTTP 303 |
| 304 | Http304 or NotModified | Response status "redirection" with HTTP 304 |
| 305 | Http305 or UseProxy | Response status "redirection" with HTTP 305 |
| 307 | Http307 or TemporaryRedirect | Response status "redirection" with HTTP 307 |
| 308 | Http308 or PermanentRedirect | Response status "redirection" with HTTP 308 |
Functions Code HTTP 4XX
| Http Code | Function Name | Description |
|---|---|---|
| 400 | Http400 or BadRequest | Response status "error" with HTTP 400 |
| 401 | Http401 or Unauthorized | Response status "error" with HTTP 401 |
| 402 | Http402 or PaymentRequired | Response status "error" with HTTP 402 |
| 403 | Http403 or Forbidden | Response status "error" with HTTP 403 |
| 404 | Http404 or NotFound | Response status "error" with HTTP 404 |
| 405 | Http405 or MethodNotAllowed | Response status "error" with HTTP 405 |
| 406 | Http406 or NotAcceptable | Response status "error" with HTTP 406 |
| 407 | Http407 or ProxyAuthenticationRequired | Response status "error" with HTTP 407 |
| 408 | Http408 or RequestTimeout | Response status "error" with HTTP 408 |
| 409 | Http409 or Conflict | Response status "error" with HTTP 409 |
| 410 | Http410 or Gone | Response status "error" with HTTP 410 |
| 411 | Http411 or LengthRequired | Response status "error" with HTTP 411 |
| 412 | Http412 or PreconditionFailed | Response status "error" with HTTP 412 |
| 413 | Http413 or RequestEntityTooLarge | Response status "error" with HTTP 413 |
| 414 | Http414 or RequestUriTooLarge | Response status "error" with HTTP 414 |
| 415 | Http415 or UnsupportedMediaType | Response status "error" with HTTP 415 |
| 416 | Http416 or RequestedRangeNotSatisfiable | Response status "error" with HTTP 416 |
| 417 | Http417 or ExpectationFailed | Response status "error" with HTTP 417 |
| 421 | Http421 or MissDirectedRequest | Response status "error" with HTTP 421 |
| 422 | Http422 or UnprocessableEntity | Response status "error" with HTTP 422 |
| 423 | Http423 or Locked | Response status "error" with HTTP 423 |
| 424 | Http424 or FailedDependency | Response status "error" with HTTP 424 |
| 426 | Http426 or UpgradeRequired | Response status "error" with HTTP 426 |
| 428 | Http428 or PreconditionRequired | Response status "error" with HTTP 428 |
| 429 | Http429 or TooManyRequests | Response status "error" with HTTP 429 |
| 431 | Http431 or RequestHeaderFieldsTooLarge | Response status "error" with HTTP 431 |
| 451 | Http451 or UnavailableForLegalReasons | Response status "error" with HTTP 451 |
Functions Code HTTP 5XX
| Http Code | Function Name | Description |
|---|---|---|
| 500 | Http500 or InternalServerError | Response status "error" with HTTP 500 |
| 501 | Http501 or NotImplemented | Response status "error" with HTTP 501 |
| 502 | Http502 or BadGateway | Response status "error" with HTTP 502 |
| 503 | Http503 or ServiceUnavailable | Response status "error" with HTTP 503 |
| 504 | Http504 or GatewayTimeout | Response status "error" with HTTP 504 |
| 505 | Http505 or HTTPVersionNotSupported | Response status "error" with HTTP 505 |
| 506 | Http506 or VariantAlsoNegotiates | Response status "error" with HTTP 506 |
| 507 | Http507 or InsufficientStorage | Response status "error" with HTTP 507 |
| 508 | Http508 or LoopDetected | Response status "error" with HTTP 508 |
| 510 | Http510 or NotExtended | Response status "error" with HTTP 510 |
| 511 | Http511 or NetworkAuthenticationRequired | Response status "error" with HTTP 511 |
Examples
404 Error example
//Execution in php return ApiResponse::NotFound(null, 'object not found!'); // or //Execution in php without params return ApiResponse::NotFound(); // or return ApiResponse::Http404();
//Response { "status": "error", "message": "Not Found", "data": null }
//HTTP Response Status Code: 404 Not Found
401 Error example
//Execution in php return ApiResponse::Unauthorized(); // or return ApiResponse::Http401();
//Response { "status": "error", "message": "Unauthorized", "data": null }
//HTTP Response Status Code: 401 Unauthorized
403 Error example
//Execution in php return ApiResponse::Forbidden(); // or return ApiResponse::Http403();
//Response { "status": "error", "message": "Forbidden", "data": null }
//HTTP Response Status Code: 403 Forbidden
Credits
License
license. Please see the license file for more information.
willywes/apiresponse 适用场景与选型建议
willywes/apiresponse 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.99k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 10 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「laravel」 「api response」 「lumen」 「apiresponse」 「Standard Api Response」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 willywes/apiresponse 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 willywes/apiresponse 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 willywes/apiresponse 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
HTTP request logger middleware for Laravel
A PSR-7 compatible library for making CRUD API endpoints
Alfabank REST API integration
Speed up a Laravel application by caching the entire response additions
Laravel DataTables Package.
Resposes with codes for Laravel PHP framework
统计信息
- 总下载量: 1.99k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-10-22