W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
這個(gè)Laravle包提供了一種代碼生成器,使得你可以加速你的開發(fā)進(jìn)程,這些生成器包括:
項(xiàng)目地址
https://github.com/laracasts/Laravel-5-Generators-Extended
安裝
使用Composer安裝這個(gè)包,編輯你項(xiàng)目的composer.json文件,在require
中添加way/generators
"require-dev": {
"way/generators": "~2.0"
}
然后,在命令行下執(zhí)行composer update
:
composer update --dev
一旦這個(gè)操作完成,就只需要最后一步,在配置文件中加入服務(wù)提供者。打開app/config/app.php
文件,添加一個(gè)新的記錄到providers
數(shù)組中.
'Way\Generators\GeneratorsServiceProvider'
這樣就可以了,你已經(jīng)安裝完成并可以運(yùn)行這個(gè)包了。運(yùn)行artisan命令行則可以在終端上看到generate
相關(guān)命令。
php artisan
使用Composer安裝這個(gè)包,編輯你項(xiàng)目的composer.json
文件,在require
中添加way/generators
"require-dev": {
"way/generators": "~3.0"
}
由于在Laravel高版本中默認(rèn)文件夾結(jié)構(gòu),需要3.0或者更高的版本,才能適應(yīng)5.0版本以上的Laravel
然后,在命令行下執(zhí)行composer update
:
composer update --dev
一旦這個(gè)操作完成,就只需要最后一步,在配置文件中加入服務(wù)提供者。打開app/config/app.php
文件,添加一個(gè)新的記錄到providers
數(shù)組中.
'Way\Generators\GeneratorsServiceProvider'
這樣就可以了,你已經(jīng)安裝完成并可以運(yùn)行這個(gè)包了。運(yùn)行artisan命令行則可以在終端上看到generate
相關(guān)命令。
php artisan
想象一下使用一個(gè)生成器加速你的工作流。而不是打開models文件夾,創(chuàng)建一個(gè)新的文件,保存它,并且在文件中添加一個(gè)class,你可以簡(jiǎn)單的運(yùn)行一個(gè)生成器命令即可完成這一系列動(dòng)作。
Laravel提供了一個(gè)遷移生成器,但是它僅僅能夠創(chuàng)建數(shù)據(jù)庫(kù)結(jié)構(gòu)。讓我們?cè)倩仡檸讉€(gè)例子,使用generate:migration
。
php artisan generate:migration create_posts_table
如果我們不指定字段配置項(xiàng),則下面這個(gè)文件將被創(chuàng)建在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');
}
}
注意:生成器能夠檢測(cè)到你正在嘗試創(chuàng)建一個(gè)表。遷移的名稱,盡量應(yīng)該是可描述的。生成器將掃描你的生成器名字的第一個(gè)單詞,并盡力確定如何繼續(xù)。例如,對(duì)于遷移create_posts_table,關(guān)鍵字"create",意味著我們應(yīng)該準(zhǔn)備必要的架構(gòu)來(lái)創(chuàng)建表。
如果你使用add_user_id_to_posts_table
代替遷移的名字,在上面的示例中,關(guān)鍵字"add
",意味著我們將添加一行到現(xiàn)有的表中,然我們看看這個(gè)生成器命令。
php artisan generate:migration add_user_id_to_posts_table
這個(gè)命令將會(huì)準(zhǔn)備一個(gè)下面這樣的樣板:
<?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) {
});
}
}
注意:這一次我們沒(méi)有做Schema::create
關(guān)鍵字
當(dāng)你在寫遷移的名字的時(shí)候,使用下面的關(guān)鍵字給生成器提供提示。
這是非常漂亮的,但是讓我們更進(jìn)一步,生成數(shù)據(jù)庫(kù)模式的同時(shí),使用fields
選項(xiàng)。
php artisan generate:migration create_posts_table --fields="title:string, body:text"
在我們解釋這個(gè)選項(xiàng)之前,讓我們先看一下輸出:
<?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
選項(xiàng),并添加這些字段聲明字段,使用逗號(hào)+空格分隔鍵值列表[key:value:option sets
],其中key
表示字段的名稱,value
表示字段的類型,option
表示制定索引或者像是unique
、nullable
這樣的屬性。 這里是一些示例:
請(qǐng)注意最后一個(gè)示例,這里我們指定了string(30)
的字符串限制。這將產(chǎn)生$table->string('username', 30)->unique()
;
使用生成器刪除表是可能的:
php artisan generate:migration delete_posts_table
作為最后一個(gè)示例i,讓我們運(yùn)行一個(gè)遷移,從tasks
表中,刪除completed
字段。
php artisan generate:migration remove_completed_from_tasks_table --fields="completed:boolean"
這一次,我們使用了"remove
"關(guān)鍵字,生成器知道它要?jiǎng)h除一個(gè)字段,并且把它添加到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
這將生成一個(gè)文件,app/models/Post.php并且寫入下面的樣板
<?php
class Post extends \Eloquent {
}
視圖生成器相當(dāng)簡(jiǎn)單。
php artisan generate:view admin.reports.index
這個(gè)命令將創(chuàng)建一個(gè)空的視圖,/app/views/admin/reports/index.blade.php
。如果提供的文件夾不存在,它會(huì)自動(dòng)幫你創(chuàng)建Seeds
生成數(shù)據(jù)[譯注:應(yīng)該是用來(lái)填充測(cè)試數(shù)據(jù)]
Laravel為我們提供了非常靈活的方式來(lái)填充表 Laravel provides us with a flexible way to seed new tables.
php artisan generate:seed users
設(shè)置你想要生成的生成文件參數(shù)。這將生成 app/database/seeds/UsersTableSeeder.php
并用一下內(nèi)容作為填充:
<?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
庫(kù)為你提供一個(gè)基本的樣板。這將是一個(gè)非常漂亮的方式來(lái)生成你的數(shù)據(jù)庫(kù)表。不要忘記使用Composer
來(lái)安裝Faker
!
關(guān)聯(lián)表[譯注:pivot 這個(gè)詞愿意是中心點(diǎn)、中樞的意思,這里翻譯成關(guān)聯(lián)表比較合適,通俗一點(diǎn)就是兩個(gè)表的mapping關(guān)系表,帶有外鍵約束]
當(dāng)你需要一個(gè)關(guān)聯(lián)表時(shí),generate:pivot
可以加速建立相應(yīng)的表。
簡(jiǎn)單的傳遞兩個(gè)需要關(guān)聯(lián)的表的名字。對(duì)于orders
和users
表,你可以:
php artisan generate:pivot orders users
這個(gè)命令將創(chuàng)建下面的遷移:
<?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');
}
}
注意:它會(huì)正確設(shè)置你提供的兩個(gè)表名,排名不分先后?,F(xiàn)在,運(yùn)行php artisan migrate來(lái)創(chuàng)建你的關(guān)聯(lián)表!
資源
generate:resource
命令將會(huì)為你坐一系列的事情:
當(dāng)你觸發(fā)這個(gè)命令,它將對(duì)每個(gè)動(dòng)作進(jìn)行問(wèn)詢。通過(guò)這個(gè)方式,你可以告訴生成器,哪些是你確實(shí)需要的。
例子
想象如果你需要?jiǎng)?chuàng)建一個(gè)方法來(lái)顯示文章。你需要手動(dòng)創(chuàng)建一個(gè)控制器,一個(gè)模型,一個(gè)數(shù)據(jù)庫(kù)遷移并且填充它,并且創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)填充…為什么不用生成器來(lái)做呢?
php artisan generate:resource post --fields="title:string, body:text"
如果你對(duì)每個(gè)詢問(wèn)說(shuō)yes,這個(gè)命令會(huì)給你如下樣板:
腳手架生成器類似于generate:resource
,除了創(chuàng)建一些初始化樣板外,同時(shí)也是為了方便。
例如,在運(yùn)行generate:scaffold post
時(shí),你的控制器模板將會(huì)將會(huì)是:
<?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');
}
}
請(qǐng)注意我們鼓勵(lì)您去修改這些自動(dòng)生成的控制器。它只是一個(gè)簡(jiǎn)單的開始。Configuration
配置
你或許想修改你的模板–自動(dòng)生成的文件是怎樣格式化的。考慮到這一點(diǎn),你需要發(fā)布你的模板,生成器將會(huì)使用它們。
php artisan generate:publish-templates
你要復(fù)制所有app/templates目錄下的模板。你可以修改這些只要你滿意它的格式。如果你喜歡不同的目錄:
php artisan generate:publish-templates --path=app/foo/bar/templates
當(dāng)你運(yùn)行generate:publish-templates
,它也會(huì)將配置發(fā)布到app/config/packages/way/generators/config/config.php
文件。這個(gè)文件看起來(lái)有點(diǎn)像:
<?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')
];
同時(shí),當(dāng)你修改這個(gè)文件的時(shí)候,注意你也可以更新每個(gè)默認(rèn)的生成器目標(biāo)目錄。
因?yàn)槟憧赡軙?huì)一次又一次的鍵入如下命令,命令別名顯然是有意義的。
# 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
文件中。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: