Skip to content

Commit

Permalink
Updated docs to 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklaw5 committed Dec 26, 2016
1 parent 7a21f70 commit c6ca3f2
Show file tree
Hide file tree
Showing 2 changed files with 322 additions and 70 deletions.
131 changes: 61 additions & 70 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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**
Expand All @@ -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:
Expand All @@ -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]"
}
]
}
Expand Down Expand Up @@ -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]"
}
]
}
Expand All @@ -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
```


Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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"
}
}
```
Expand All @@ -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
Loading

0 comments on commit c6ca3f2

Please sign in to comment.