百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
L

lumen-generators

> 后端框架
开源

卢门和拉拉维尔5号的发电机组集合.

349 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

卢门和拉拉维尔5号的发电机组集合.

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 wn/lumen-generators

Then add the service provider in the file app/Providers/AppServiceProvider.phplike the following:

php
public function register()
{
    if ($this->app->environment() == 'local') {
        $this->app->register('Wn\Generators\CommandsServiceProvider');
    }
}

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:

php artisan wn:resource task "name;string;required;fillable project_id;integer:unsigned;numeric;fillable,key due;date;;date" --add=timestamps --belongs-to=project

will generate these files:

app/Task.php

php
 "required",
		"project_id" => "numeric",
	];

	public function project()
	{
		return $this->belongsTo("App\Project");
	}

}

app/Http/Controllers/RESTActions.php

…

app/Http/Controllers/TasksController.php

php
get('task', 'TasksController@all');
$app->get('task/{id}', 'TasksController@get');
$app->post('task', 'TasksController@add');
$app->put('task/{id}', 'TasksController@put');
$app->delete('task/{id}', 'TasksController@remove');

database/migrations/date_time_create_tasks.php

php
increments('id');
            $table->string('name');
            $table->integer('project_id')->unsigned();
            $table->date('due');
            $table->foreign('project_id')
                ->references('id')
                ->on('projects');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::drop('tasks');
    }
}

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 wn:model command is used to generate a model class based on Eloquent. It has the following syntax:

wn:model name [--fillable=...] [--dates=...] [--has-many=...] [--has-one=...] [--belongs-to=...] [--belongs-to-many=...] [--rules=...] [--timestamps=false] [--path=...] [--soft-deletes=true] [--force=true]
  • name: the name of the model.

php artisan wn:model Task generates the following:

php
hasMany("Tests\Tmp\Account");
	}

	public function owner()
	{
		return $this->belongsTo("App\User");
	}

	public function number()
	{
		return $this->hasOne("Tests\Tmp\Phone");
	}

	public function tags()
	{
		return $this->belongsToMany("Tests\Tmp\Tag")->withTimestamps();
	}
  • --rules: specifies the validation rules of the model's fields. The syntax is field1=rules1 field2=rules2 ....
php artisan wn:model TestingModel --rules="name=required age=integer|min:13 email=email|unique:users,email_address"`

gives:

php
// ...
	public static $rules = [
		"name" => "required",
		"age" => "integer|min:13",
		"email" => "email|unique:users,email_address",
	];
  • --timestamps: Enables timestamps on the model. Giving --timestamps=false will add public $timestamps = false; to the generated model. The default value is true.

  • --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:

TestingModel model already exists; use --force option to override it !

Migration Generator

The wn:migration command is used to generate a migration to create a table with schema. It has the following syntax:

wn:migration table [--schema=...] [--add=...] [--keys=...] [--force=true] [--file=...]
  • 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:.... The type could be text, string.50, decimal.5,2 for example. Modifiers can be unique or nullable for example. See documentation for the list of available types and modifiers.

php artisan wn:migration tasks --schema="amount:decimal.5,2:after.'size':default.8 title:string:nullable"

gives:

php
increments('id');
            $table->decimal('amount', 5, 2)->after('size')->default(8);
            $table->string('title')->nullable();
            // Constraints declaration

        });
    }

    public function down()
    {
        Schema::drop('tasks');
    }
}
  • --add: Specifies additional columns like timestamps, softDeletes, rememberToken and nullableTimestamps.

  • --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.

php artisan wn:migration tasks --keys="category_type_id user_id:identifier:members:cascade"

gives:

php
//...
$table->foreign('category_type_id')
    ->references('id')
    ->on('category_types');

$table->foreign('user_id')
    ->references('identifier')
    ->on('members')
    ->onDelete('cascade');

Pivot Table Generator

The wn:pivot-table command is used to generate a migration to create a pivot table between two models. It has the following syntax:

wn:pivot-table model1 model2 [--add=...] [--force=true] [--file=...]
  • 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 and nullableTimestamps.

  • --file: The migration file name. By default the name follows the patern date_time_create_table_name.php.

php artisan wn:pivot-table Tag Project --add=timestamps

gives:

php
increments('id');
            $table->integer('project_id')->unsigned()->index();
            $table->integer('tag_id')->unsigned()->index();
            $table->foreign('project_id')
                ->references('id')
                ->on('projects');
            $table->foreign('tag_id')
                ->references('id')
                ->on('tags');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::drop('project_tag');
    }
}

Controller Generator

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] [--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 wn:controller Task --no-routes gives:

php
get('project-type', 'ProjectTypesController@all');
$app->get('project-type/{id}', 'ProjectTypesController@get');
$app->post('project-type', 'ProjectTypesController@add');
$app->put('project-type/{id}', 'ProjectTypesController@put');
$app->delete('project-type/{id}', 'ProjectTypesController@remove');

php artisan wn:route project-type --laravel adds the following routes:

php
Route::get('project-type', 'ProjectTypesController@all');
Route::get('project-type/{id}', 'ProjectTypesController@get');
Route::post('project-type', 'ProjectTypesController@add');
Route::put('project-type/{id}', 'ProjectTypesController@put');
Route::delete('project-type/{id}', 'ProjectTypesController@remove');

Resource Generator

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 [--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 and nullableTimestamps of the migration and if the list contains no timestamps, the model with contain public $timestamps = false;.

  • --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.

  • --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 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 [--path=...] [--force=true]

This generator is smart enough to add foreign keys automatically when finding a b

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHPcode-generatorlaravellumenrest-api

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类后端框架
定价开源

> 相关工具

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架