xethron / laravel-4-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 4.2及以下版本
首先,通过Composer安装此包。编辑项目的composer.json
文件,以要求way/generators
。
"require-dev": {
"way/generators": "~2.0"
}
不支持Laravel 5,因为现在该框架已包含许多内置生成器。
接下来,从终端更新Composer
composer update --dev
此操作完成后,最后一步是添加服务提供者。打开app/config/app.php
,并将新项目添加到提供者数组中。
'Way\Generators\GeneratorsServiceProvider'
这就完成了!您已准备好开始。从终端运行artisan
命令,以查看新的generate
命令。
php artisan
接下来,从终端更新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 库的基本样板代码。这是种子您的 DB 表的好方法。别忘了通过 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
文件中。