2017 © Pedro Peláez
 

library laravel-api

An API rendering package for Laravel 5

image

hpolthof/laravel-api

An API rendering package for Laravel 5

  • Wednesday, January 11, 2017
  • by hpolthof
  • Repository
  • 1 Watchers
  • 3 Stars
  • 13 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 0 % Grown

The README.md

Uniform API rendering for Laravel 5

Latest Stable Version License Total Downloads, (*1)

This package was created for an internal project, but as the idea is reusable, I encourage others to make use of this package., (*2)

The main goal was to create an uniform way of presenting API output, and the creation of a layer between data structure and output., (*3)

Installation

Require this package with composer:, (*4)

composer require hpolthof/laravel-api

Add the follow service provider to your config/app.php:, (*5)

'Hpolthof\LaravelAPI\APIServiceProvider',

Middleware

A new middleware named api.errors will be added to your list of available middleware., (*6)

If you're using Laravel 5.3, this middleware will also be added into the api middleware group., (*7)

Usage

To use this package you should implement the Hpolthof\LaravelAPI\Contracts\ShouldMorphAPI interface onto an Eloquent model., (*8)

You'll have to implement the function bindAPI() and have to return an instance of Hpolthof\LaravelAPI\Binding., (*9)

Like this:, (*10)

public function bindAPI()
{
    return Binding::create([
        'street' => $this->street,
        'street_nr' => $this->number,
        'street_suffix' => $this->suffix,
        'postcode' => $this->zip,
        'city' => $this->city,
    ]);
}

In a controller you can then return the following:, (*11)

public function index()
{
    $items = Address::all();
    return \Response::api($items);
}

public function show($id)
{
    $item = Address::find($id);
    return \Response::api($item);
}

The response would look something like this:, (*12)

{
    "header": {
        "request": {
            "location": "http:\/\/localhost:8000\/api\/addresses",
            "method": "GET",
            "parameters": []
        },
        "response": {
            "status": 200,
            "error": null,
            "timestamp": "2016-11-28 14:09:35"
        }
    },
    "content": [
        {
            "street": "Van der Polweg",
            "street_nr": 17,
            "street_suffix": "",
            "postcode": "3384HD",
            "city": "Amersfoort",
        },
        {
            "street": "Van der Polweg",
            "street_nr": 15,
            "street_suffix": "",
            "postcode": "3384HD",
            "city": "Amersfoort",
        }
    ]
}

Error messages

Sometimes you'll need to force an error to the user, this can be done by throwing an exception. The package also provides some specific exceptions that should be used where relevant., (*13)

Hpolthof\LaravelAPI\Exceptions\AccessDeniedException Hpolthof\LaravelAPI\Exceptions\BadRequestException Hpolthof\LaravelAPI\Exceptions\NotFoundException Hpolthof\LaravelAPI\Exceptions\NotImplementedException, (*14)

This would result in something like:, (*15)

json { "header": { "request": { "location": "http:\/\/localhost:8000\/api\/addresses", "method": "GET", "parameters": [] }, "response": { "status": 403, "error": "Forbidden", "timestamp": "2016-11-28 15:05:29" } } }, (*16)

The Versions

11/01 2017

v0.1.4

0.1.4.0

An API rendering package for Laravel 5

  Sources   Download

GPL2

The Requires

  • php >=5.4.0

 

laravel api json rest

05/01 2017

dev-master

9999999-dev

An API rendering package for Laravel 5

  Sources   Download

GPL2

The Requires

  • php >=5.4.0

 

laravel api json rest

05/01 2017

v0.1.3

0.1.3.0

An API rendering package for Laravel 5

  Sources   Download

GPL2

The Requires

  • php >=5.4.0

 

laravel api json rest

05/01 2017

v0.1.2

0.1.2.0

An API rendering package for Laravel 5

  Sources   Download

GPL2

The Requires

 

laravel api json rest

28/11 2016

v0.1.1

0.1.1.0

An API rendering package for Laravel 5

  Sources   Download

GPL2

The Requires

 

laravel api json rest

28/11 2016

v0.1.0

0.1.0.0

An API rendering package for Laravel 5

  Sources   Download

GPL2

The Requires

 

laravel api json rest