Laravel API/Scaffold/CRUD Generator (Laravel5)
, (*1)
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., (*2)
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., (*3)
The artisan command can generate the following items:
* Migration File
* Model
* Repository (optional)
* Controller
* View
* index.blade.php
* show.blade.php
* create.blade.php
* edit.blade.php
* fields.blade.php
* adjusts routes.php, (*4)
And your simple CRUD API is ready in mere seconds., (*5)
Here is the full documentation., (*6)
Upgrade Guide from 1.0 to 1.1 or 1.2
-
Take a backup of your config/generator.php, (*7)
-
Delete your config/generator.php, (*8)
-
Change version in composer.json, (*9)
"require": {
"mitulgolakiya/laravel-api-generator": "1.2.*"
}
-
Run composer update., (*10)
-
Run publish command again., (*11)
php artisan vendor:publish --provider="Mitul\Generator\GeneratorServiceProvider"
-
Replace your custom paths again in config/generator.php., (*12)
-
Enjoy Upgrade :), (*13)
Steps to Get Started
-
Add this package to your composer.json:, (*14)
"require": {
"mitulgolakiya/laravel-api-generator": "1.2.*"
}
-
Run composer update, (*15)
composer update
-
Add the ServiceProviders to the providers array in config/app.php.
As we are using these two packages illuminate/html & laracasts/flash as a dependency.
so we need to add those ServiceProviders as well., (*16)
'Illuminate\View\ViewServiceProvider',
'Illuminate\Html\HtmlServiceProvider',
'Laracasts\Flash\FlashServiceProvider',
'Mitul\Generator\GeneratorServiceProvider'
Also for convenience, add these facades in alias array in config/app.php., (*17)
'Form' => 'Illuminate\Html\FormFacade',
'HTML' => 'Illuminate\Html\HtmlFacade',
'Flash' => 'Laracasts\Flash\Flash'
-
Publish generator.php, (*18)
php artisan vendor:publish --provider="Mitul\Generator\GeneratorServiceProvider"
-
Fire artisan command to generate API, Scaffold with CRUD views or both API as well as CRUD views., (*19)
php artisan mitul.generator:api ModelName
php artisan mitul.generator:scaffold ModelName
php artisan mitul.generator:scaffold_api ModelName
e.g., (*20)
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
-
Enter the fields with options
, (*21)
-
And you are ready to go. :), (*22)
Documentation
Generator Config file
Config file (config/generator.php) contains path for all generated files, (*23)
path_migration - Path where Migration file to ge generated
path_model - Path where Model file to ge generated
path_repository - Path where Repository file to ge generated
path_controller - Path where Controller file to ge generated
path_api_controller - Path where API Controller file to ge 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)
, (*24)
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
, (*25)
model_extend - Use custom Model extend
model_extend_namespace - Namespace of custom extended model
model_extend_class - Class name to extend
, (*26)
api_prefix - API Prefix, (*27)
Here is the input for the fields by which you can specify Input., (*28)
fieldName:fieldType,options:fieldOptions
e.g.,, (*29)
email:string:unique
email:string:unique,default('me@mitul.me')
title:string,100
price:flat,8,4
Parameters will be in the same sequence as Blueprint class function for all types.
Option will be printed as it is given in input except unique & primary., (*30)
API Response Structure
Remember: This response structure is based on the most of my API response structure, you can change it to your API response after file generation in controller., (*31)
Success, (*32)
{
"flag":true,
"message":"success message",
"data":{}
}
data can be anything as per response., (*33)
Failure, (*34)
{
"flag":false,
"message":"failure message",
"code": 0
"data":{}
}
data will be optional. And code will be error code., (*35)
Generated Views
While generating scaffold, all views are created with basic CRUD functionality., (*36)
Views will be created in resources/views/modelName folder,, (*37)
index.blade.php - Main Index file for listing records
create.blade.php - To insert a new record
edit.blade.php - To edit a record
fields.blade.php - Common file of all model fields, which will be used create and edit record
show.blade.php - To display a record
Screenshots
Command Execution
, (*38)
Generated Files & routes.php
, (*39)
Migration File
, (*40)
Model File
, (*41)
Repository File
, (*42)
Controller File
, (*43)
View Files
, (*44)
Credits
This API Generator is created by Mitul Golakiya., (*45)
Bugs & Forks are welcomed :), (*46)