2017 © Pedro Peláez
 

library laravel-api-generator

Two simple tools for building REST APIs with fractal: console generator and API skeleton

image

arrilot/laravel-api-generator

Two simple tools for building REST APIs with fractal: console generator and API skeleton

  • Sunday, July 22, 2018
  • by Arrilot
  • Repository
  • 5 Watchers
  • 32 Stars
  • 1,319 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 7 Forks
  • 0 Open issues
  • 9 Versions
  • 10 % Grown

The README.md

Total Downloads Scrutinizer Quality Score MIT License, (*1)

Laravel Api Generator (No longer supported)

Two simple tools for building REST APIs with fractal: console generator and API skeleton, (*2)

Introduction

This package provides two features, (*3)

  1. Console generator which creates Controller, Fractal Transformer and routes in a single command., (*4)

  2. Basic REST API skeleton that can be really helpful if you need something standard. It's 100% optional., (*5)

If you do not use Fractal for your transformation layer, this package is probably not the right choice for you., (*6)

Installation

1) Run composer require arrilot/laravel-api-generator, (*7)

2) Register a service provider in the app.php configuration file, (*8)

 [
    ...
    'Arrilot\Api\ServiceProvider',
],
?>

3) Copy basic folder structure to app/Api cp -R vendor/arrilot/laravel-api-generator/templates/Api app/Api and check what you got there. If you need you can use different paths later., (*9)

Usage

Generator

The only console command that is added is artisan make:api <ModelName>., (*10)

Imagine you need to create a rest api to list/create/update etc users from users table. To achieve that you need to do lots of boilerplate operations - create controller, transformer, set up needed routes., (*11)

php artisan make:api User does all the work for you., (*12)

1) You may have noticed that after installation you already have a routes file app/Api/routes.php which looks like that:, (*13)

<?php

Route::group(['prefix' => 'api/v1', 'namespace' => 'App\Api\Controllers'], function () {
    //
});

Feel free to change it if you like., (*14)

The generator adds Route::resource('users', 'UserController'); to the end of this file., (*15)

<?php

Route::group(['prefix' => 'api/v1', 'namespace' => 'App\Api\Controllers'], function () {
    //
    Route::resource('users', 'UserController');
});

As you can see it's smart enough to detect some route groups and treat this situation properly., (*16)

2) Then the generator creates a controller that extends base api controller., (*17)

<?php namespace App\Api\Controllers;

use App\User;
use App\Api\Transformers\UserTransformer;

class UserController extends Controller
{
    /**
     * Eloquent model.
     *
     * @return \Illuminate\Database\Eloquent\Model
     */
    protected function model()
    {
        return new User;
    }

    /**
     * Transformer for the current model.
     *
     * @return \League\Fractal\TransformerAbstract
     */
    protected function transformer()
    {
        return new UserTransformer;
    }
}

You can customize this stub as much as you want., (*18)

3) Finally the generator creates a fractal Transformer, (*19)

<?php namespace App\Api\Transformers;

use App\User;
use League\Fractal\TransformerAbstract;

class UserTransformer extends TransformerAbstract
{
    /**
     * Resource key.
     *
     * @var string
     */
    protected $resourceKey = null;

    /**
     * Turn this item object into a generic array.
     *
     * @param $item
     * @return array
     */
    public function transform(User $item)
    {
        return [
            'id'         => (int)$item->id,
            'created_at' => (string)$item->created_at,
            'updated_at' => (string)$item->updated_at,
        ];
    }
}

This stub is customizable too., (*20)

Skeleton

You may have noticed that controller which has just been generated includes two public methods - model() and transformer() That's because those methods are the only thing that you need in your controller to set up a basic REST API if you use the Skeleton., (*21)

The list of routes that are available out of the box:, (*22)

  1. GET api/v1/users
  2. GET api/v1/users/{id}
  3. POST api/v1/users
  4. PUT api/v1/users/{id}
  5. DELETE api/v1/users/{id}

Request and respone format is json Fractal includes are supported via $_GET['include']. Validation rules for create and update can be set by overwriting rulesForCreate and rulesForUpdate in your controller., (*23)

This skeleton is not a silver bullet but in many cases it can be either exactly what you need or can be used as a decent starting point for your api., (*24)

You can check https://github.com/Arrilot/laravel-api-generator/blob/master/src/Skeleton/BaseController.php for more info., (*25)

If you don't like the Skeleton just stop inheriting it in the base controller - Api\Controllers\Controller and overwrite the controller stub in your config to remove model() and transformer() methods., (*26)

Configuration

All paths and generator's stubs are configurable., (*27)

https://github.com/Arrilot/laravel-api-generator/blob/master/src/config/config.php, (*28)

The Versions

22/07 2018

dev-master

9999999-dev https://github.com/Arrilot/laravel-api-generator

Two simple tools for building REST APIs with fractal: console generator and API skeleton

  Sources   Download

MIT

The Requires

 

by Nekrasov Ilya

api rest generator fractal

22/07 2018

1.1.3

1.1.3.0 https://github.com/Arrilot/laravel-api-generator

Two simple tools for building REST APIs with fractal: console generator and API skeleton

  Sources   Download

MIT

The Requires

 

by Nekrasov Ilya

api rest generator fractal

16/08 2015

1.1.2

1.1.2.0 https://github.com/Arrilot/laravel-api-generator

Two simple tools for building REST APIs with fractal: console generator and API skeleton

  Sources   Download

MIT

The Requires

 

by Nekrasov Ilya

api rest generator fractal

01/06 2015

1.1.1

1.1.1.0 https://github.com/Arrilot/laravel-api-generator

Two simple tools for building REST APIs with fractal: console generator and API skeleton

  Sources   Download

MIT

The Requires

 

by Nekrasov Ilya

api rest generator fractal

31/05 2015

1.1.0

1.1.0.0 https://github.com/Arrilot/laravel-api-generator

Two simple tools for building REST APIs with fractal: console generator and API skeleton

  Sources   Download

MIT

The Requires

 

by Nekrasov Ilya

api rest generator fractal

31/05 2015

1.0.3

1.0.3.0 https://github.com/Arrilot/laravel-api-generator

Two simple tools for building REST APIs with fractal: console generator and API skeleton

  Sources   Download

MIT

The Requires

 

by Nekrasov Ilya

api rest generator fractal

31/05 2015

1.0.2

1.0.2.0 https://github.com/Arrilot/laravel-api-generator

Two simple tools for building REST APIs with fractal: console generator and API skeleton

  Sources   Download

MIT

The Requires

 

by Nekrasov Ilya

api rest generator fractal

25/05 2015

1.0.1

1.0.1.0 https://github.com/Arrilot/laravel-api-generator

Laravel api console generator utilizing Fractal package. Includes basic API skeleton too.

  Sources   Download

MIT

The Requires

 

by Nekrasov Ilya

api rest generator fractal

24/05 2015