已知的 / 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
This package is auto-updated.
Last update: 2024-09-23 09:15:00 UTC
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 库。这是一种很好的方式来填充您的数据库表。别忘了通过 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
文件中。