Download the PHP package flickerleap/lumen-generators without Composer
On this page you can find all versions of the php package flickerleap/lumen-generators. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download flickerleap/lumen-generators
More information about flickerleap/lumen-generators
Files in flickerleap/lumen-generators
Package lumen-generators
Short Description A collection of generators for Lumen and Laravel 5.
License MIT
Informations about the package lumen-generators
Lumen generators
A collection of generators for Lumen and Laravel 5.
Contents
-
Why ?
-
Installation
-
Quick Usage
-
Detailed Usage
-
Model Generator
-
Migration Generator
-
Pivot Table Generator (Since version 1.1.0)
-
Controller Generator
-
Routes Generator
-
Resource Generator
- Multiple Resources From File
-
-
Testing
-
Development Notes
- Contributing
Why ?
I installed Lumen and wanted to use it to create a REST API (since this is the main usage of Lumen). But I didn't find commands which will speed up my workflow. That's why I created this package and included useful commands to build a RESTful API.
This packages was mainly built to be used with Lumen, but it should work fine with Laravel 5 too.
Installation
Add the generators package to your composer.json by running the command:
composer require flickerleap/lumen-generators
Then add the service provider in the file app/Providers/AppServiceProvider.php
like the following:
Don't forget to include the application service provider on your bootstrap/app.php
and to enable Eloquent and Facades if you are using Lumen
If you run the command php artisan list
you will see the list of added commands:
Quick Usage
To generate a RESTful resource for your application (model, migration, controller and RESTful routes), you simply need to run one single command. For example:
will generate these files:
app/Task.php
app/Http/Controllers/RESTActions.php
app/Http/Controllers/TasksController.php
app/Http/routes.php
database/migrations/date_time_create_tasks.php
Now simply run the migration and you are ready to go.
More then that, you can generate multiple resources with only one command ! Click here to see an example
Detailed Usage
Model Generator
The flickerleap:model
command is used to generate a model class based on Eloquent. It has the following syntax:
- name: the name of the model.
php artisan flickerleap:model Task
generates the following:
- --fillable: the mass fillable fields of the model separated with commas.
php artisan flickerleap:model Task --fillable=name,title
gives:
- --dates: the date fields of the model, these will be converted automatically to
Carbon
instances on retrieval.
php artisan flickerleap:model Task --dates=started_at,published_at
gives:
- --path: specifies the path where to store the model php file. This path is used to know the namespace of the model. The default value is "app".
php artisan flickerleap:model Task --path="app/Http/Models"
gives:
- --has-one, --has-many, --belongs-to and --belongs-to-many: the relationships of the model following the syntax
relation1:model1,relation2:model2,...
. If themodel
is missing, it will be deducted from the relation's name. If themodel
is given without a namespace, it will be considered having the same namespace as the model being generated.
gives:
- --rules: specifies the validation rules of the model's fields. The syntax is
field1=rules1 field2=rules2 ...
.
gives:
-
--timestamps: Enables timestamps on the model. Giving
--timestamps=false
will addpublic $timestamps = false;
to the generated model. The default value istrue
. -
--soft-deletes: Adds
Illuminate\Database\Eloquent\SoftDeletes
trait to the model. This is disabled by default. - --force: tells the generator to override the existing file. By default, if the model file already exists, it will not be overriden and the output will be something like:
Migration Generator
The flickerleap:migration
command is used to generate a migration to create a table with schema. It has the following syntax:
-
table: the name of the table to create.
-
--file: The migration file name (to speicify only for testing purpose). By default the name follows the patern
date_time_create_tableName_table.php
. - --schema: the schema of the table using the syntax
field1:type.arg1,ag2:modifier1:modifier2.. field2:...
. Thetype
could betext
,string.50
,decimal.5,2
for example. Modifiers can beunique
ornullable
for example. See documentation for the list of available types and modifiers.
gives:
-
--add: Specifies additional columns like
timestamps
,softDeletes
,rememberToken
andnullableTimestamps
. - --keys: the foreign keys of the table following the syntax
field:column:table:on_delete:on_update ...
. Thecolumn
is optional ("id" by default). Thetable
is optional if the field follows the naming conventionsingular_table_name_id
.on_delete
andon_update
are optional too.
gives:
Pivot Table Generator
The flickerleap:pivot-table
command is used to generate a migration to create a pivot table between two models. It has the following syntax:
-
model1 and model2: names of the two models (or the two tables if the models don't follow the naming conventions)
-
--add: Specifies additional columns like
timestamps
,softDeletes
,rememberToken
andnullableTimestamps
. - --file: The migration file name. By default the name follows the patern
date_time_create_table_name.php
.
gives:
Controller Generator
There are two commands for controllers. The first one is flickerleap:controller:rest-actions
which generates a trait used by all generated controllers. This trait includes the following methods:
-
all()
: returns all the model entries as JSON. -
get($id)
: returns a specific model by id as JSON. -
add(Request $request)
: adds a new model or returns validation errors as JSON. -
put(Request $request, $id)
: updates a model or returns validation errors as JSON. remove($id)
: removes an entry by id.
Note that the trait doesn't use the common used methods on Laravel (like index, store, ...) to avoid conflicts. Which enables you to use this trait with controllers you already have in your application.
The second command is flickerleap:controller
which actually generates the controller. The syntax of this command is flickerleap:controller model [--no-routes] [--force=true]
.
-
model: Name of the model (with namespace if not
App
). -
--no-routes: Since routes are generated by default for the controller, this option is used to tell the generator "do not generate routes !".
-
--force: tells the generator to override the existing file.
- --laravel: create Laravel style routes
php artisan flickerleap:controller Task --no-routes
gives:
Routes Generator
The flickerleap:route
command is used to generate RESTfull routes for a controller. It has the following syntax:
flickerleap:route resource [--controller=...] [--force=true]
-
resource: the resource name to use in the URLs.
-
--controller: the corresponding controller. If missing it's deducted from the resource name.
-
--force: tells the generator to override the existing file.
- --laravel: create Laravel style routes
php artisan flickerleap:route project-type
adds the following routes:
php artisan flickerleap:route project-type --laravel
adds the following routes:
Resource Generator
The flickerleap:resource
command makes it very easy to generate a RESTful resource. It generates a model, migration, controller and routes. The syntax is : flickerleap:resource name fields [--add=...] [--has-many=...] [--has-one=...] [--belongs-to=...] [--migration-file=...] [--path=...] [--force=true]
-
name: the name of the resource used in the URLs and to determine the model, table and controller names.
-
fields: specifies the fields of the resource along with schema and validation rules. It follows the syntax
name;schema;rules;tags ...
-
name: the name of the field
-
schema: the schema following the syntax in the model generator. (note that the name is not part of the schema like on the model generator)
-
rules: the validation rules
-
tags: additional tags separated by commas. The possible tags are:
-
fillable
: add this field to the fillable array of the model. -
date
: add this field to the dates array of the model. key
: this field is a foreign key.
-
-
-
--add: Specifies additional columns like
timestamps
,softDeletes
,rememberToken
andnullableTimestamps
of the migration and if the list contains no timestamps, the model with containpublic $timestamps = false;
. -
--has-one, --has-many and --belongs-to are the same as for the
flickerleap:model
command. -
--migration-file: passed to the
flickerleap:migration
as the--file
option. -
--path: Defines where to store the model file as well as its namespace.
-
--force: tells the generator to override the existing file.
- --laravel: create Laravel style routes
Multiple Resources From File
The flickerleap:resources
(note the "s" in "resources") command takes the generation process to an other level by parsing a file and generating multiple resources based on it. The syntax is
This generator is smart enough to add foreign keys automatically when finding a belongsTo relation. It also generates pivot tables for belongsToMany relations automatically.
The file given to the command should be a valid YAML file ( for the moment, support of other types like XML or JSON could be added in the future). An example is the following:
-
--path: Defines where to store the model files as well as their namespace.
- --laravel: create Laravel style routes
Testing
To test the generators, I included a fresh lumen installation under the folder lumen-test
to which I added this package and have written some acceptance tests using Codeception. To run tests you just have to execute the install.sh
to install dependencies then execute test.sh
.
Development Notes
-
Comming versions
-
Seeder and Test generators
-
Requested Feature: Custom Templates
-
Requested Feature: Fractal integration
-
Requested Feature: Add possibility to not run migrations when using
flickerleap:resources
- Documentation: Adding examples
-
-
Version 1.3.3
-
Version 1.3.2
- Bug Fixed: softDeletes not added to model
-
Version 1.3.1
-
Version 1.3.0
-
Requested Feature: Disabling timestamps
- Requested Feature: Lumen 5.3 routes support
-
-
Version 1.2.0
-
Tests fixed.
-
Bug fixed: Undefined index: factory
-
Feature added: Check if file already exists before generating it
-
Feature added: Support for additional columns like nullableTimestamps() and softDeletes() in migrations
- Feature added: Specifying namespace for
flickerleap:resource
andflickerleap:resources
-
-
Version 1.1.1
- Pivot table generation from the
flickerleap:resources
command bug fixed.
- Pivot table generation from the
-
Version 1.1.0
-
Pivot table generator added.
-
belongsToMany relationship added to model generator.
-
Multiple resources generator adds foreign keys for belongsTo relationships automatically.
-
Multiple resources generator adds pivot tables for belongsToMany relationships automatically.
- Generated migrations file names changed to be supported by
migrate
command.
-
-
Version 1.0.0
-
Model Generator
-
Migration Generator
-
Controller Generator
-
Routes Generator
-
Resource Generator
- Multiple Resources From File
-
Contributing
Pull requests are welcome :D
All versions of lumen-generators with dependencies
illuminate/console Version ^5.1
illuminate/filesystem Version ^5.1
fzaninotto/faker Version ^1.5