-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
322 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,17 +2,17 @@ Larapi | |
====== | ||
A simple Laravel 5 package for handling common HTTP API responses in JSON form. | ||
|
||
# Installation | ||
To install the package, simply add the following to your Laravel installation's `composer.json` file: | ||
## Docs | ||
You can find [v2.x docs here](docs/v2x-docs.md). For the lastest (v3.x) docs see below. | ||
|
||
```json | ||
"require": { | ||
"laravel/framework": "5.*", | ||
"nicklaw5/larapi": "2.0.*" | ||
}, | ||
## Installation | ||
Pull in Larapi via composer: | ||
|
||
```bash | ||
$ composer require nicklaw5/larapi | ||
``` | ||
|
||
Run `composer update` to pull in the files. Then, add the following **Service Provider** to your `providers` array in your `config/app.php` file: | ||
Then, add the following **Service Provider** to your `providers` array in your `config/app.php` file: | ||
|
||
```php | ||
'providers' => array( | ||
|
@@ -21,13 +21,14 @@ Run `composer update` to pull in the files. Then, add the following **Service Pr | |
); | ||
``` | ||
|
||
# Usage | ||
## Usage | ||
###Succes Responses### | ||
Available responses: | ||
```php | ||
Larapi::respondOk(); // 200 HTTP Response | ||
Larapi::respondCreated(); // 201 HTTP Response | ||
Larapi::respondAccepted(); // 202 HTTP Response | ||
Larapi::ok(); // 200 HTTP Response | ||
Larapi::created(); // 201 HTTP Response | ||
Larapi::accepted(); // 202 HTTP Response | ||
Larapi::noContent(); // 204 HTTP Response | ||
``` | ||
|
||
**Example: Return HTTP OK** | ||
|
@@ -38,16 +39,14 @@ This: | |
|
||
Route::get('/', function() | ||
{ | ||
return Larapi::respondOk(); | ||
return Larapi::ok(); | ||
}); | ||
``` | ||
|
||
will return: | ||
```json | ||
{ | ||
"code":200, | ||
"status":"OK", | ||
"message":"success" | ||
"success": true | ||
} | ||
``` | ||
with these headers: | ||
|
@@ -69,26 +68,24 @@ Route::get('/', function() | |
['id' => 2, 'name' => 'Jane Doe', 'email' => '[email protected]'] | ||
]; | ||
|
||
return Larapi::respondOk($data); | ||
return Larapi::ok($data); | ||
}); | ||
``` | ||
|
||
will return: | ||
```json | ||
{ | ||
"code":200, | ||
"status":"OK", | ||
"message":"success", | ||
"success": true, | ||
"response": [ | ||
{ | ||
"id":1, | ||
"name":"John Doe", | ||
"email":"[email protected]" | ||
"id": 1, | ||
"name": "John Doe", | ||
"email": "[email protected]" | ||
}, | ||
{ | ||
"id":2, | ||
"name":"Jane Doe", | ||
"email":"[email protected]" | ||
"id": 2, | ||
"name": "Jane Doe", | ||
"email": "[email protected]" | ||
} | ||
] | ||
} | ||
|
@@ -117,25 +114,23 @@ Route::get('/', function() | |
'Header-2' => 'Header-2 Data' | ||
]; | ||
|
||
return Larapi::respondOk($data, $headers); | ||
return Larapi::ok($data, $headers); | ||
}); | ||
``` | ||
will return: | ||
```json | ||
{ | ||
"code":200, | ||
"status":"OK", | ||
"message":"success", | ||
"success": true, | ||
"response": [ | ||
{ | ||
"id":1, | ||
"name":"John Doe", | ||
"email":"[email protected]" | ||
"id": 1, | ||
"name": "John Doe", | ||
"email": "[email protected]" | ||
}, | ||
{ | ||
"id":2, | ||
"name":"Jane Doe", | ||
"email":"[email protected]" | ||
"id": 2, | ||
"name": "Jane Doe", | ||
"email": "[email protected]" | ||
} | ||
] | ||
} | ||
|
@@ -152,15 +147,16 @@ Header-2: Header-2 Data | |
|
||
Available responses: | ||
```php | ||
Larapi::respondBadRequest(); // 400 HTTP Response | ||
Larapi::respondUnauthorized(); // 401 HTTP Response | ||
Larapi::respondForbidden(); // 403 HTTP Response | ||
Larapi::respondNotFound(); // 404 HTTP Response | ||
Larapi::respondMethodNotAllowed(); // 405 HTTP Response | ||
Larapi::respondConflict(); // 409 HTTP Response | ||
Larapi::respondInternalError(); // 500 HTTP Response | ||
Larapi::respondNotImplemented(); // 501 HTTP Response | ||
Larapi::respondNotAvailable(); // 503 HTTP Response | ||
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 | ||
``` | ||
|
||
|
||
|
@@ -172,15 +168,13 @@ This: | |
|
||
Route::get('/', function() | ||
{ | ||
return Larapi::respondBadRequest(); | ||
return Larapi::badRequest(); | ||
}); | ||
``` | ||
will return: | ||
```json | ||
{ | ||
"code":400, | ||
"status":"Bad Request", | ||
"message":"error" | ||
"success": false | ||
} | ||
``` | ||
with these headers: | ||
|
@@ -200,19 +194,15 @@ Route::get('/', function() | |
$errorCode = 4001; | ||
$errorMessage = 'Invalid email address.'; | ||
|
||
return Larapi::respondBadRequest($errorMessage, $errorCode); | ||
return Larapi::badRequest($errorMessage, $errorCode); | ||
}); | ||
``` | ||
will return: | ||
```json | ||
{ | ||
"code":400, | ||
"status":"Bad Request", | ||
"message":"error", | ||
"response":{ | ||
"errorCode":4001, | ||
"errorMessage":"Invalid email address." | ||
} | ||
"success": false, | ||
"error_code": 4001, | ||
"error": "Invalid email address." | ||
} | ||
``` | ||
with these headers: | ||
|
@@ -221,7 +211,7 @@ HTTP/1.1 400 Bad Request | |
Content-Type: application/json | ||
``` | ||
|
||
**Example: Return HTTP Bad Request with Custom Response Headers** | ||
**Example: Return HTTP Bad Request with An Array of Errors and Custom Response Headers** | ||
|
||
This: | ||
```php | ||
|
@@ -230,25 +220,26 @@ This: | |
Route::get('/', function() | ||
{ | ||
$errorCode = 4001; | ||
$errorMessage = 'Invalid email address.'; | ||
$errors = [ | ||
'email' => 'Invalid email address', | ||
'password' => 'Not enough characters', | ||
]; | ||
|
||
$headers = [ | ||
'Header-1' => 'Header-1 Data', | ||
'Header-2' => 'Header-2 Data' | ||
]; | ||
|
||
return Larapi::respondBadRequest($errorMessage, $errorCode, $headers); | ||
return Larapi::badRequest($errors, null, $headers); | ||
}); | ||
``` | ||
will return: | ||
```json | ||
{ | ||
"code":400, | ||
"status":"Bad Request", | ||
"message":"error", | ||
"response":{ | ||
"errorCode":4001, | ||
"errorMessage":"Invalid email address." | ||
"success": false, | ||
"errors": { | ||
"email": "Invalid email address", | ||
"password": "Not enough characters" | ||
} | ||
} | ||
``` | ||
|
@@ -260,8 +251,8 @@ Header-1: Header-1 Data | |
Header-2: Header-2 Data | ||
``` | ||
|
||
# License | ||
Larapi is licensed under the terms of the MIT License. | ||
## License | ||
Larapi is licensed under the terms of the [MIT License](LICENSE). | ||
|
||
# TODO | ||
- test, test, test | ||
## TODO | ||
- test, test, test |
Oops, something went wrong.