nicklaw5/larapi
最新稳定版本:3.0.1
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
统计信息
- 总下载量: 1.54k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 11
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-11-06