2017 © Pedro Peláez
 

library api-foundation

A basis for your REST API

image

sinclairt/api-foundation

A basis for your REST API

  • Thursday, September 7, 2017
  • by sinclairt
  • Repository
  • 1 Watchers
  • 0 Stars
  • 45 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 14 Versions
  • 0 % Grown

The README.md

API Foundation

A simple trait to build RESTful controllers using Fractal., (*1)

Installation

Register the service provider in config/app.php providers array: Sinclair\ApiFoundation\Providers\ApiFoundationServiceProvider::class, (*2)

Usage

Use the \Sinclair\ApiFoundation\Traits\ApiFoundation trait inside your API controllers to create RESTful controllers., (*3)

This is written as a trait to prevent inheritance issues, so feel free to overwrite any of the methods., (*4)

ApiFoundation ships with a default transformer, which turns the Eloquent model to an array, and with an overload __call method to handle includes for additional relationships., (*5)

You will need to __construct the controller to inject a resource implementation of Sinclair\Repository\Contracts\Repository and the transformer you would like to use. You can optionally set the resource name for Fractal to use., (*6)

There are fluent setters for the transformer, resource name, and repository should you need them., (*7)

Available Methods
  • index GET paginated collection
  • filter POST paginated collection
  • store POST item
  • show GET item
  • update PUT/PATCH item
  • destroy DELETE item
  • restore GET item
Using Laravel/Eloquent to take the load off!

It is recommended to use the properties that Eloquent provides to reduce the code in the Transformers. * Use the $hidden/$visible properties to control what is seen inside an array. * Use the $casts property to control the type the fields are cast to for arrays. See here for more detail. * Use the $with property to control which relations are eager loaded by default., (*8)

class User extends Model
{
    protected $fillable = ['name', 'email', 'password', 'api_token', 'is_admin'];

    protected $hidden = ['password'];
    // or
    protected $visible = ['name', 'email', 'api_token', 'is_admin'];

    protected $casts = [
        'name'      => 'string',
        'posts'     => 'collection',
        'is_admin'  => 'boolean'
    ];

    protected $with = [
        'posts'
    ];

    public function posts()
    {
        return $this->hasMany(App\Posts::class);
    }
}

It would be sensible to add the token auth driver to the api middleware group in App\Http\Kernel.php:, (*9)

'api' => [
            'throttle:60,1',
            'auth:token'
        ],

But you are free to use your own authorisation driver., (*10)

I strongly recommend setting up an API route group such as:, (*11)

Route::group(['middleware' => 'api', 'namespace' => 'Api'], function()
{
    Route::get('api/v1/user/{user}/restore', [
        'as'   => 'api.v1.user.restore',
        'uses' => 'UserController@restore'
    ]);

    Route::post('api/v1/user/{user}/filter', [
        'as'   => 'api.v1.user.filter',
        'uses' => 'UserController@filter'
    ]);

    Route::resource('/api/v1/user', 'UserController', ['except' => ['create', 'edit']]);
}):

Use form requests for each resource so you're validation is abstracted away from the controller as well. I'd expect you to over write the store and update methods, so you can inject your form requests., (*12)

Finally, let's use Route Model Binding, here's a quick script I use for this in App\Providers\RouteServiceProvider:, (*13)

protected $bindings = [
    'user'
];

public function boot( Router $router )
{
    foreach ( $this->bindings as $model )
    {
        $router->bind(strtolower($model), function ( $value ) use ( $model )
        {
            $model = app(studly_case($model));

            $model = in_array(SoftDeletes::class, class_uses($model)) ? $model->withTrashed()
                                                                              ->find($value) : $model->find($value);

            // the abort is optional
            if ( is_null($model) )
                abort(403);

            return $model;
        });
    }

    parent::boot($router);
}

The Versions

07/09 2017

dev-master

9999999-dev

A basis for your REST API

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tom Sinclair

07/09 2017

2.0.2

2.0.2.0

A basis for your REST API

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tom Sinclair

07/09 2017
29/03 2017
19/01 2017

1.2.5

1.2.5.0

A basis for your REST API

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tom Sinclair

06/01 2017

1.2.3

1.2.3.0

A basis for your REST API

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tom Sinclair

19/12 2016

1.2.2

1.2.2.0

A basis for your REST API

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tom Sinclair

19/12 2016

1.2.1

1.2.1.0

A basis for your REST API

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tom Sinclair

19/12 2016

1.2.0

1.2.0.0

A basis for your REST API

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tom Sinclair

28/11 2016

1.1.0

1.1.0.0

A basis for your REST API

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tom Sinclair

09/09 2016

1.0.1

1.0.1.0

A basis for your REST API

  Sources   Download

MIT

The Requires

 

The Development Requires

by Tom Sinclair

06/09 2016

1.0.0

1.0.0.0

A basis for your REST API

  Sources   Download

MIT

The Requires

 

by Tom Sinclair

21/07 2016

0.0.1

0.0.1.0

A basis for your REST API

  Sources   Download

MIT

The Requires

 

by Tom Sinclair