2017 © Pedro Peláez
 

library larapi

A simple Laravel 5 class for handling json api responses.

image

nicklaw5/larapi

A simple Laravel 5 class for handling json api responses.

  • Friday, February 3, 2017
  • by nicklaw5
  • Repository
  • 4 Watchers
  • 9 Stars
  • 1,049 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 12 Versions
  • 17 % Grown

The README.md

Larapi

A simple Laravel 5 package for handling common HTTP API responses in JSON form., (*1)

Docs

You can find v2.x docs here. For the lastest (v3.x) docs see below., (*2)

Installation

Pull in Larapi via composer:, (*3)

$ composer require nicklaw5/larapi

Then, add the following Service Provider to your providers array in your config/app.php file:, (*4)

'providers' => array(
    ...
    Larapi\LarapiServiceProvider::class,
);

Usage

Succes Responses

Available responses:, (*5)

Larapi::ok();           // 200 HTTP Response
Larapi::created();      // 201 HTTP Response
Larapi::accepted();     // 202 HTTP Response
Larapi::noContent();    // 204 HTTP Response

Example: Return HTTP OK, (*6)

This:, (*7)

// app/Http/routes.php

Route::get('/', function()
{
    return Larapi::ok();
});

will return:, (*8)

{
    "success": true
}

with these headers:, (*9)

HTTP/1.1 200 OK
Content-Type: application/json

Example: Return HTTP OK with Response Data, (*10)

This:, (*11)

// 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:, (*12)

{
    "success": true,
    "response": [
        {
            "id": 1,
            "name": "John Doe",
            "email": "john@doe.com"
        },
        {
            "id": 2,
            "name": "Jane Doe",
            "email": "jane@doe.com"
        }
    ]
}

with these headers:, (*13)

HTTP/1.1 200 OK
Content-Type: application/json

Example: Return HTTP OK with Custom Response Headers, (*14)

This:, (*15)

// 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:, (*16)

{
    "success": true,
    "response": [
        {
            "id": 1,
            "name": "John Doe",
            "email": "john@doe.com"
        },
        {
            "id": 2,
            "name": "Jane Doe",
            "email": "jane@doe.com"
        }
    ]
}

with these headers:, (*17)

HTTP/1.1 200 OK
Content-Type: application/json
Header-1: Header-1 Data
Header-2: Header-2 Data

Error Responses

Available responses:, (*18)

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, (*19)

This:, (*20)

// app/Http/routes.php

Route::get('/', function()
{
    return Larapi::badRequest();
});

will return:, (*21)

{
    "success": false
}

with these headers:, (*22)

HTTP/1.1 400 Bad Request
Content-Type: application/json

Example: Return HTTP Bad Request with Custom Application Error Message, (*23)

This:, (*24)

// app/Http/routes.php

Route::get('/', function()
{
    $errorCode = 4001;
    $errorMessage = 'Invalid email address.';

    return Larapi::badRequest($errorMessage, $errorCode);
});

will return:, (*25)

{
    "success": false,
    "error_code": 4001,
    "error": "Invalid email address."
}

with these headers:, (*26)

HTTP/1.1 400 Bad Request
Content-Type: application/json

Example: Return HTTP Bad Request with An Array of Errors and Custom Response Headers, (*27)

This:, (*28)

// 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:, (*29)

{
    "success": false,
    "errors": {
        "email": "Invalid email address",
        "password": "Not enough characters"
    }
}

with these headers:, (*30)

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., (*31)

TODO

  • test, test, test

The Versions

03/02 2017

dev-master

9999999-dev

A simple Laravel 5 class for handling json api responses.

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

laravel api json response

03/02 2017

3.0.1

3.0.1.0

A simple Laravel 5 class for handling json api responses.

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

laravel api json response

26/12 2016

3.0

3.0.0.0

A simple Laravel 5 class for handling json api responses.

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

laravel api json response

23/06 2016

2.1

2.1.0.0

A simple Laravel 5 class for handling json api responses.

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

laravel api json response

05/01 2016

2.0

2.0.0.0

A simple Laravel 5 class for handling json api responses.

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

laravel api json response

10/11 2015

1.2

1.2.0.0

A simple Laravel 5 class for handling json api responses.

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

laravel api json response

07/11 2015

1.1.1

1.1.1.0

A simple Laravel 5 class for handling json api responses.

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

laravel api framework json

07/11 2015

1.1

1.1.0.0

A simple Laravel 5 class for handling json api responses.

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

laravel api framework json

06/11 2015

1.0.3

1.0.3.0

A simple Laravel 5 class for handling json api responses.

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

laravel api framework json

06/11 2015

1.0.2

1.0.2.0

A simple Laravel 5 class for handling json api responses.

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

laravel api framework json

06/11 2015

1.0.1

1.0.1.0

A simple Laravel 5 class for handling json api responses.

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

laravel api framework json

06/11 2015

1.0

1.0.0.0

A simple Laravel 5 class for handling json api responses.

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

laravel api framework json