way / generators
快速生成资源、迁移、模型等。
Requires
- php: >=5.4.0
- illuminate/support: ~5.0
Requires (Dev)
- behat/behat: ~2.5.1
- behat/mink: ~1.5.0
- behat/mink-extension: ~1.2.0
- behat/mink-goutte-driver: ~1.0.9
- behat/mink-selenium2-driver: ~1.1.1
- phpspec/phpspec: ~2.0
- phpunit/phpunit: ~3.7
README
这个Laravel包提供各种生成器以加快开发过程。这些生成器包括
generate:model
generate:view
generate:controller
generate:seed
generate:migration
generate:pivot
generate:resource
generate:scaffold
安装
Laravel 5
如果您使用的是Laravel 5,请使用此包。
Laravel 4.2及以下
首先,通过Composer安装此包。编辑项目的composer.json
文件,以需要way/generators
。
"require-dev": {
"way/generators": "~2.0"
}
接下来,从终端更新Composer
composer update --dev
此操作完成后,最后一步是添加服务提供者。打开config/app.php
,并将新项添加到提供者数组中。
'Way\Generators\GeneratorsServiceProvider'
这就完成了!您已经准备好开始使用了。从终端运行artisan
命令,查看新的generate
命令。
php artisan
使用方法
将生成器视为加快您工作流程的简单方法。您无需打开模型目录,创建新文件,保存并添加类,只需运行单个生成命令即可。
迁移
Laravel提供了一个迁移生成器,但它仅在创建模式(或表字段)之前停止。让我们回顾几个使用generate:migration
的例子。
php artisan generate:migration create_posts_table
如果没有指定fields
选项,以下文件将在app/database/migrations
中创建。
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; class CreatePostsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('posts', function(Blueprint $table) { $table->increments('id'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::drop('posts'); } }
请注意,生成器足够智能,可以检测您正在尝试创建一个表。当命名迁移时,尽量使其尽可能详细。迁移生成器将检测迁移名称中的第一个单词,并尽可能确定如何进行。因此,对于create_posts_table
,关键字是"create",这意味着我们应该准备必要的模式来创建表。
如果您使用类似add_user_id_to_posts_table
的迁移名称,那么关键字是"add",这表示我们打算向现有表添加行。让我们看看它会生成什么。
php artisan generate:migration add_user_id_to_posts_table
这将准备以下样板代码
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; class AddUserIdToPostsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::table('posts', function(Blueprint $table) { }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('posts', function(Blueprint $table) { }); } }
请注意,这次我们没有做Schema::create
。
关键字
在编写迁移名称时,使用以下关键字为生成器提供提示。
create
或make
(例如:create_users_table
)add
或insert
(例如:add_user_id_to_posts_table
)remove
(例如:remove_user_id_from_posts_table
)delete
或drop
(例如:delete_users_table
)
生成模式
这很棒,但让我们进一步,使用fields
选项生成模式。
php artisan generate:migration create_posts_table --fields="title:string, body:text"
在我们分析这个新选项之前,让我们看看输出结果
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; class CreatePostsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('posts', function(Blueprint $table) { $table->increments('id'); $table->string('title'); $table->text('body'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::drop('posts'); } }
很好!请注意以下几点
- 生成器将自动将
id
设置为主键。 - 它解析了
fields
选项,并添加了这些字段。 - 删除方法足够智能,能够实现反向操作,应完全删除该表。
要声明字段,请使用逗号加空格分隔的键:值:选项集合列表,其中 键
是字段的名称,值
是 列类型,而 选项
是指定索引等的一种方式,例如 unique
或 nullable
。以下是一些示例
--fields="first:string, last:string"
--fields="age:integer, yob:date"
--fields="username:string:unique, age:integer:nullable"
--fields="name:string:default('John Doe'), bio:text:nullable"
--fields="username:string(30):unique, age:integer:nullable:default(18)"
请注意最后一个示例,我们指定了字符限制: string(30)
。这将生成 $table->string('username', 30)->unique();
可以通过以下命令删除表
php artisan generate:migration delete_posts_table
作为最后的演示,让我们运行一个迁移来从 tasks
表中删除 completed
字段。
php artisan generate:migration remove_completed_from_tasks_table --fields="completed:boolean"
这次,由于我们使用了 "remove" 关键字,生成器理解它应该删除一个列,并在 down()
方法中将其添加回来。
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; class RemoveCompletedFromTasksTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::table('tasks', function(Blueprint $table) { $table->dropColumn('completed'); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('tasks', function(Blueprint $table) { $table->boolean('completed'); }); } }
模型
php artisan generate:model Post
这将创建文件,app/models/Post.php
并插入以下样板
<?php class Post extends \Eloquent { }
视图
视图生成器相当简单。
php artisan generate:view admin.reports.index
此命令将创建一个空视图,/app/views/admin/reports/index.blade.php
。如果提供的目录树不存在,它将为您创建。
种子
Laravel 以灵活的方式提供了为新表设置种子数据的方法。
php artisan generate:seed users
将参数设置为需要种子文件的表名。这将生成 app/database/seeds/UsersTableSeeder.php
并填充它
<?php // Composer: "fzaninotto/faker": "v1.3.0" use Faker\Factory as Faker; class UsersTableSeeder extends Seeder { public function run() { $faker = Faker::create(); foreach(range(1, 10) as $index) { User::create([ ]); } } }
这将为您提供一个基本的样板,使用流行的 Faker 库。这是一种很好的方法来为您的数据库表设置种子。别忘了通过 Composer 引入 Faker!
枢轴
当你需要一个新的关联表时,generate:pivot
表可以加快创建适当迁移的过程。
只需传递需要连接的关联表的两个表名。对于 orders
和 users
,你可能这样做
php artisan generate:pivot orders users
这将创建以下迁移
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; class CreateOrderUserTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('order_user', function(Blueprint $table) { $table->increments('id'); $table->integer('order_id')->unsigned()->index(); $table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade'); $table->integer('user_id')->unsigned()->index(); $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::drop('order_user'); } }
请注意,它正确地设置了根据您提供的两个表按字母顺序排列的表名。现在,运行 php artisan migrate
来创建您的关联表!
资源
generate:resource
命令将为您做很多事情
- 生成模型
- 生成索引、显示、创建和编辑视图
- 生成控制器
- 生成带有模式迁移
- 生成表种子
- 迁移数据库
在触发此命令时,您将需要确认这些操作中的每一个。这样,您可以定制生成以符合您的特定需求。
示例
假设您需要构建一种显示帖子的方式。虽然您可以手动创建控制器、创建模型、创建迁移并填充模式,然后创建表种子...为什么不让生成器来做呢?
php artisan generate:resource post --fields="title:string, body:text"
如果您对每个确认都说“是”,则此单个命令将为您提供以下样板
- app/models/Post.php
- app/controllers/PostsController.php
- app/database/migrations/timestamp-create_posts_table.php(包括模式)
- app/database/seeds/PostsTableSeeder.php
脚手架
脚手架生成器与 generate:resource
类似,但它将添加一些开始样板到这些文件中,作为便利。
例如,当运行 generate:scaffold post
时,您的控制器样板将是
<?php class PostsController extends \BaseController { /** * Display a listing of posts * * @return Response */ public function index() { $posts = Post::all(); return View::make('posts.index', compact('posts')); } /** * Show the form for creating a new post * * @return Response */ public function create() { return View::make('posts.create'); } /** * Store a newly created post in storage. * * @return Response */ public function store() { $validator = Validator::make($data = Input::all(), Post::$rules); if ($validator->fails()) { return Redirect::back()->withErrors($validator)->withInput(); } Post::create($data); return Redirect::route('posts.index'); } /** * Display the specified post. * * @param int $id * @return Response */ public function show($id) { $post = Post::findOrFail($id); return View::make('posts.show', compact('post')); } /** * Show the form for editing the specified post. * * @param int $id * @return Response */ public function edit($id) { $post = Post::find($id); return View::make('posts.edit', compact('post')); } /** * Update the specified resource in storage. * * @param int $id * @return Response */ public function update($id) { $post = Post::findOrFail($id); $validator = Validator::make($data = Input::all(), Post::$rules); if ($validator->fails()) { return Redirect::back()->withErrors($validator)->withInput(); } $post->update($data); return Redirect::route('posts.index'); } /** * Remove the specified resource from storage. * * @param int $id * @return Response */ public function destroy($id) { Post::destroy($id); return Redirect::route('posts.index'); } }
请注意,您被鼓励修改此生成的控制器。它只是提供了一个起点。
配置
您可能想要修改模板 - 生成文件的格式。为此,您需要发布生成器在幕后引用的模板。
php artisan generate:publish-templates
这将把所有模板复制到您的 app/templates
目录。您可以随意修改这些模板以符合您所需的格式。如果您更喜欢不同的目录
php artisan generate:publish-templates --path=app/foo/bar/templates
当你运行generate:publish-templates
命令时,它也会将配置发布到app/config/packages/way/generators/config/config.php
。这个文件看起来可能是这样的
<?php return [ /* |-------------------------------------------------------------------------- | Where the templates for the generators are stored... |-------------------------------------------------------------------------- | */ 'model_template_path' => '/Users/jeffreyway/Desktop/generators-testing/app/templates/model.txt', 'scaffold_model_template_path' => '/Users/jeffreyway/Desktop/generators-testing/app/templates/scaffolding/model.txt', 'controller_template_path' => '/Users/jeffreyway/Desktop/generators-testing/app/templates/controller.txt', 'scaffold_controller_template_path' => '/Users/jeffreyway/Desktop/generators-testing/app/templates/scaffolding/controller.txt', 'migration_template_path' => '/Users/jeffreyway/Desktop/generators-testing/app/templates/migration.txt', 'seed_template_path' => '/Users/jeffreyway/Desktop/generators-testing/app/templates/seed.txt', 'view_template_path' => '/Users/jeffreyway/Desktop/generators-testing/app/templates/view.txt', /* |-------------------------------------------------------------------------- | Where the generated files will be saved... |-------------------------------------------------------------------------- | */ 'model_target_path' => app_path('models'), 'controller_target_path' => app_path('controllers'), 'migration_target_path' => app_path('database/migrations'), 'seed_target_path' => app_path('database/seeds'), 'view_target_path' => app_path('views') ];
此外,当你在这个文件中时,请注意,你也可以更新每个生成器的默认目标目录。
快捷键
由于你可能需要反复输入这些命令,因此创建别名是有意义的。
# Generator Stuff alias g:m="php artisan generate:model" alias g:c="php artisan generate:controller" alias g:v="php artisan generate:view" alias g:s="php artisan generate:seed" alias g:mig="php artisan generate:migration" alias g:r="php artisan generate:resource"
例如,这些可以存储在~/.bash_profile
或~/.bashrc
文件中。