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 wn/lumen-generators`
Then add the service provider in the file `app/Providers/AppServiceProvider.php`like the following:
To generate a RESTful resource for your application (model, migration, controller and RESTful routes), you simply need to run one single command. For example:
- **--has-one**, **--has-many**, **--belongs-to** and **--belongs-to-many**: the relationships of the model following the syntax `relation1:model1,relation2:model2,...`. If the `model` is missing, it will be deducted from the relation's name. If the `model` is given without a namespace, it will be considered having the same namespace as the model being generated.
- **--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:...`. The `type` could be `text`, `string.50`, `decimal.5,2` for example. Modifiers can be `unique` or `nullable` for example. [See documentation](http://laravel.com/docs/5.1/migrations#creating-columns) for the list of available types and modifiers.
- **--keys**: the foreign keys of the table following the syntax `field:column:table:on_delete:on_update ...`. The `column` is optional ("id" by default). The `table` is optional if the field follows the naming convention `singular_table_name_id`. `on_delete` and `on_update` are optional too.
There are two commands for controllers. The first one is `wn: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 `wn:controller` which actually generates the controller. The syntax of this command is `wn:controller model [--no-routes]`.
- **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 !".
The `wn:resource` command makes it very easy to generate a RESTful resource. It generates a model, migration, controller and routes. The syntax is : `wn:resource name fields [--has-many=...] [--has-one=...] [--belongs-to=...] [--migration-file=...]`
- **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.
- **--has-one**, **--has-many** and **--belongs-to** are the same as for the `wn:model` command.
- **--migration-file**: passed to the `wn:migration` as the `--file` option.
The `wn: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
```
wn:resources filename
```
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:
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](http://codeception.com/). To run tests you just have to execute the `install.sh` to install dependencies then execute `test.sh`.