nicklaw5/larapi
Composer 安装命令:
composer require nicklaw5/larapi
包简介
A simple Laravel 5 class for handling json api responses.
README 文档
README
A simple Laravel 5 package for handling common HTTP API responses in JSON form.
Docs
You can find v2.x docs here. For the lastest (v3.x) docs see below.
Installation
Pull in Larapi via composer:
$ composer require nicklaw5/larapi
Then, add the following Service Provider to your providers array in your config/app.php file:
'providers' => array( ... Larapi\LarapiServiceProvider::class, );
Usage
###Succes Responses### Available responses:
Larapi::ok(); // 200 HTTP Response Larapi::created(); // 201 HTTP Response Larapi::accepted(); // 202 HTTP Response Larapi::noContent(); // 204 HTTP Response
Example: Return HTTP OK
This:
// app/Http/routes.php Route::get('/', function() { return Larapi::ok(); });
will return:
{
"success": true
}
with these headers:
HTTP/1.1 200 OK
Content-Type: application/json
Example: Return HTTP OK with Response Data
This:
// app/Http/routes.php Route::get('/', function() { $data = [ ['id' => 1, 'name' => 'John Doe', 'email' => 'john@doe.com'], ['id' => 2, 'name' => 'Jane Doe', 'email' => 'jane@doe.com'] ]; return Larapi::ok($data); });
will return:
{
"success": true,
"response": [
{
"id": 1,
"name": "John Doe",
"email": "john@doe.com"
},
{
"id": 2,
"name": "Jane Doe",
"email": "jane@doe.com"
}
]
}
with these headers:
HTTP/1.1 200 OK
Content-Type: application/json
Example: Return HTTP OK with Custom Response Headers
This:
// app/Http/routes.php Route::get('/', function() { $data = [ ['id' => 1, 'name' => 'John Doe', 'email' => 'john@doe.com'], ['id' => 2, 'name' => 'Jane Doe', 'email' => 'jane@doe.com'] ]; $headers = [ 'Header-1' => 'Header-1 Data', 'Header-2' => 'Header-2 Data' ]; return Larapi::ok($data, $headers); });
will return:
{
"success": true,
"response": [
{
"id": 1,
"name": "John Doe",
"email": "john@doe.com"
},
{
"id": 2,
"name": "Jane Doe",
"email": "jane@doe.com"
}
]
}
with these headers:
HTTP/1.1 200 OK
Content-Type: application/json
Header-1: Header-1 Data
Header-2: Header-2 Data
###Error Responses###
Available responses:
Larapi::badRequest(); // 400 HTTP Response Larapi::unauthorized(); // 401 HTTP Response Larapi::forbidden(); // 403 HTTP Response Larapi::notFound(); // 404 HTTP Response Larapi::methodNotAllowed(); // 405 HTTP Response Larapi::conflict(); // 409 HTTP Response Larapi::unprocessableEntity(); // 422 HTTP Response Larapi::internalError(); // 500 HTTP Response Larapi::notImplemented(); // 501 HTTP Response Larapi::notAvailable(); // 503 HTTP Response
Example: Return HTTP Bad Request
This:
// app/Http/routes.php Route::get('/', function() { return Larapi::badRequest(); });
will return:
{
"success": false
}
with these headers:
HTTP/1.1 400 Bad Request
Content-Type: application/json
Example: Return HTTP Bad Request with Custom Application Error Message
This:
// app/Http/routes.php Route::get('/', function() { $errorCode = 4001; $errorMessage = 'Invalid email address.'; return Larapi::badRequest($errorMessage, $errorCode); });
will return:
{
"success": false,
"error_code": 4001,
"error": "Invalid email address."
}
with these headers:
HTTP/1.1 400 Bad Request
Content-Type: application/json
Example: Return HTTP Bad Request with An Array of Errors and Custom Response Headers
This:
// app/Http/routes.php Route::get('/', function() { $errorCode = 4001; $errors = [ 'email' => 'Invalid email address', 'password' => 'Not enough characters', ]; $headers = [ 'Header-1' => 'Header-1 Data', 'Header-2' => 'Header-2 Data' ]; return Larapi::badRequest($errors, null, $headers); });
will return:
{
"success": false,
"errors": {
"email": "Invalid email address",
"password": "Not enough characters"
}
}
with these headers:
HTTP/1.1 200 OK
Content-Type: application/json
Header-1: Header-1 Data
Header-2: Header-2 Data
License
Larapi is licensed under the terms of the MIT License.
TODO
- test, test, test
nicklaw5/larapi 适用场景与选型建议
nicklaw5/larapi 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.54k 次下载、GitHub Stars 达 11, 最近一次更新时间为 2015 年 11 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「json」 「api」 「response」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 nicklaw5/larapi 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nicklaw5/larapi 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 nicklaw5/larapi 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Kinikit - PHP Application development framework MVC component
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
HTTP request logger middleware for Laravel
A PSR-7 compatible library for making CRUD API endpoints
Class to generate a standard structure for api json responses
统计信息
- 总下载量: 1.54k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 11
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-11-06