demency/laravel-api-generator

A API generator for Laravel.

3.0 2020-08-10 03:10 UTC

This package is auto-updated.

Last update: 2020-09-10 03:31:48 UTC


README

One of the main objectives of agility is make the most of the progress with the minimum energy. This package allows you to generate the base code necessary to have an API with useful features.

Features

  • Model, migration, controller, observer and requests automatically generated.

Planned

  • Resource and collection implementations.
  • Pagination and filtering.

Installation

You can install this package using the following command:

composer require demency/laravel-api-generator

Usage

The idea is to simplify the entire task in a command that generates all the stub code.

php artisan api:generate Car \
	--fields="body:text,user_id:bigInteger" \
	--storeFields="body" \
	--updateFields="body" \
	--storeRules="body:required" \
	--updateRules="body:required"

Once the command is executed, you will get an output with all the instructions performed.

Car model created
Car controller created
Car Index request created
Car Store request created
Car Show request created
Car Update request created
Car Destroy request created
Car migration created
Command executed successfully

To continue, you must execute the migrations created.

php artisan migrate

Once migrated the stub, you must add the routes in your api.php file:

// Public API, not related user resource.
Route::apiResource('cars', 'Api\CarController');

// Private API, only authenticated user.
Route::apiResource('cars', 'Api\CarController')->middleware('auth:api');

The command prompt to you if this resource is related to App/User. If your answer was yes, you must include the created observer in your AppServicProvider.php as the following example:

<?php

use App\Car;
use App\Observers\CarObserver;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Car::observe(CarObserver::class);
    }
}

Options

There are currently a limited number of options available:

  • name: The model name.
  • --fields: The fields of model.
  • --storeFields: The fields inserted extracted from request post data.
  • --storeRules: The validation rules of store request.
  • --updateFields: The fields updated extracted from request post data.
  • --updateRules: The validation rules of update request.

Disclaimer

This package is under development.