Skip to content

Laravel API/Scaffold/CRUD Generator including Controller, Repository, Model, Migrations, routes.php update.

License

Notifications You must be signed in to change notification settings

jozefbalun/laravel-api-generator

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel API/Scaffold/CRUD Generator (Laravel5.1)

Latest Stable Version Total Downloads Monthly Downloads Daily Downloads Latest Unstable Version License

For Laravel 5.0, check out 1.3 branch

I enjoy creating API's and I have worked on many projects that required them. But the problem I always faced was setting up all the boilerplate code. For example each end point needs a migration, model, controller, repository, and on and on. I wanted a way to streamline this process and that is how this package was born.

This API generator allows you to use artisan commands to automatically generate all these files saving you time. Not only does it auto generate the files but it will set the namespaces.

The artisan command can generate the following items:

  • Migration File
  • Model
  • Repository
  • Controller
  • View
    • index.blade.php
    • table.blade.php
    • show.blade.php
    • show_fields.blade.php
    • create.blade.php
    • edit.blade.php
    • fields.blade.php
  • adjusts routes.php

And your simple CRUD and APIs are ready in mere seconds.

Here is the full documentation.

Upgrade Guide.

Documentation is in process...

Documentation

  1. Installation
  2. Configuration
  3. Publish & Initialization
  4. Generator
  5. Supported Field Types
  6. Customization
    1. Base Controller
    2. Customize Templates
    3. Dingo API Integration
  7. Options
    1. Paginate Records
    2. Model Soft Deletes
    3. Fields From File
    4. Custom Table Name
    5. Skip Migration
  8. Generator from existing tables

Installation

  1. Add this package to your composer.json:

     "repositories": [
         {
             "type": "git",
             "url": "https://github.com/mitulgolakiya/laracast-flash"
         }
     ],
     "require": {
         "laracasts/flash": "dev-master",
         "laravelcollective/html": "5.1.*@dev",
         "bosnadev/repositories": "dev-master",
         "mitulgolakiya/laravel-api-generator": "dev-master"
     }
    
  2. Run composer update

     composer update
    
  3. Add the ServiceProviders to the providers array in config/app.php.
    As we are using these two packages laravelcollective/html & laracasts/flash as a dependency.
    so we need to add those ServiceProviders as well.

     Collective\Html\HtmlServiceProvider::class,
     Laracasts\Flash\FlashServiceProvider::class,
     Mitul\Generator\GeneratorServiceProvider::class,
    

    Also for convenience, add these facades in alias array in config/app.php.

     'Form'      => Collective\Html\FormFacade::class,
     'Html'      => Collective\Html\HtmlFacade::class,
     'Flash'     => Laracasts\Flash\Flash::class
    

Configuration

Publish Configuration file generator.php.

    php artisan vendor:publish --provider="Mitul\Generator\GeneratorServiceProvider"

Config file (config/generator.php) contains path for all generated files

base_controller - Base Controller for all Controllers

path_migration - Path where Migration file to be generated
path_model - Path where Model file to be generated
path_repository - Path where Repository file to be generated
path_controller - Path where Controller file to be generated
path_api_controller - Path where API Controller file to be generated
path_views - Path where views will be created
path_request - Path where request file will be created
path_routes - Path of routes.php (if you are using any custom routes file)
path_api_routes - Path of api_routes.php (this file will contain all api routes)

namespace_model - Namespace of Model
namespace_repository - Namespace of Repository
namespace_controller - Namespace of Controller
namespace_api_controller - Namespace of API Controller
namespace_request - Namespace for Request

model_extend_class - Extend class of Models

api_prefix - API Prefix api_version - API Version

use_dingo_api - Integrate APIs with dingo/api package

Publish & Initialization

Mainly, we need to do three basic things to get started.

  1. Publish some common views like errors.blade.php & paginate.blade.php.

  2. Publish api_routes.php which will contain all our api routes.

  3. Init routes.php for api routes. We need to include api_routes.php into main routes.php.

     php artisan mitul.generator:publish
    

Generator

Fire artisan command to generate API, Scaffold with CRUD views or both API as well as CRUD views.

Generate API:

    php artisan mitul.generator:api ModelName

Generate CRUD Scaffold:

    php artisan mitul.generator:scaffold ModelName

Generate CRUD Scaffold with API:

    php artisan mitul.generator:scaffold_api ModelName

e.g.

php artisan mitul.generator:api Project
php artisan mitul.generator:api Post

php artisan mitul.generator:scaffold Project
php artisan mitul.generator:scaffold Post

php artisan mitul.generator:scaffold_api Project
php artisan mitul.generator:scaffold_api Post

Here is the sample fields input json

Supported HTML Field Types

Here is the list of supported field types with options:

  • text
  • textarea
  • password
  • email
  • file
  • checkbox
  • radio:male,female,option3,option4
  • number
  • date
  • select:India,USA

Customization

Base Controller

If you want to use your own base controller or want to extend/modify default AppBaseController then you can have following options:

  1. If you want to use another controller (recommended to extends AppBaseController with new controller) as base controller then modify base_controller value in config/generator.php

  2. If you want to modify AppBaseController then,

    1. Publish AppBaseController in your controllers path

      php artisan mitul.generator:publish --baseController

    2. Modify the content of AppBaseController.php and set it as a base_controller in config/generator.php

Customize Templates

To use your own custom templates,

  1. Publish templates to /resources/api-generator-templates

     php artisan mitul.generator:publish --templates
    
  2. Leave only those templates that you want to change. Remove the templates that do not plan to change.

Dingo API Integration

Coming soon...

Options

Paginate Records

To paginate records, you can specify paginate option, e.g.

    php artisan mitul.generator:api Post --paginate=10

Model Soft Deletes

To use SoftDelete, use softDelete option,

    php artisan mitul.generator:api Post --softDelete

Fields From File

If you want to pass fields from file then you can create fields json file and pass it via command line. Here is the sample fields.json

You have to pass option --fieldsFile=absolute_file_path_or_path_from_base_directory with command. e.g.

     php artisan mitul.generator:scaffold_api Post --fieldsFile="/Users/Mitul/laravel-api-generator/fields.json"
     php artisan mitul.generator:scaffold_api Post --fieldsFile="fields.json"

Custom Table Name

You can also specify your own custom table name by,

    php artisan mitul.generator:api Post --tableName=custom_table_name

Skip Migration

You can also skip migration generation,

    php artisan mitul.generator:api Post --skipMigration

Generator from existing tables

To use generator with existing table, you can specify --fromTable option. --tableName option is required and you need to specify table name.

Just make sure, you have installed doctrine/dbal package.

Limitation: As of now it is not fully working (work is in progress). It will not create migration file. You need to tweak some of the things in your generated files like timestamps, primary key etc.

    php artisan mitul.generator:api Post --fromTable --tableName=posts

Credits

This API Generator is created by Mitul Golakiya.

Bugs & Forks are welcomed :)

About

Laravel API/Scaffold/CRUD Generator including Controller, Repository, Model, Migrations, routes.php update.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%