標籤:style blog http color io os ar 檔案 sp
上一篇已經介紹開發環境的搭建,如果有問題可以在文章後留言。
這篇將從項目實戰開發,一步一步瞭解laravel架構。
在開發mvc項目時,models都是第一步。
下面就從建模開始。
1.實體關聯圖,
由於不知道php有什麼好的建模工具,這裡我用的vs ado.net實體模型資料建模
下面開始laravel編碼,編碼之前首先得設定資料庫串連,在app/config/database.php檔案
‘mysql‘ => array(‘driver‘ => ‘mysql‘,‘read‘ => array( ‘host‘ => ‘127.0.0.1:3306‘, ), ‘write‘ => array( ‘host‘ => ‘127.0.0.1:3306‘ ),‘database‘ => ‘test‘,‘username‘ => ‘root‘,‘password‘ => ‘root‘,‘charset‘ => ‘utf8‘,‘collation‘ => ‘utf8_unicode_ci‘,‘prefix‘ => ‘‘,),
配置好之後,需要用到artisan工具,這是一個php命令工具在laravel目錄中
首先需要要通過artisan建立一個遷移 migrate ,這點和asp.net mvc幾乎是一模一樣
在laravel目錄中 shfit+右鍵開啟命令視窗 輸入artisan migrate:make create_XXXX 會在app/database/migrations檔案下產生一個帶時間戳記首碼的遷移檔案
代碼:
<?phpuse Illuminate\Database\Schema\Blueprint;use Illuminate\Database\Migrations\Migration;class CreateTablenameTable extends Migration {/** * Run the migrations. * * @return void */public function up(){ }/** * Reverse the migrations. * * @return void */public function down(){}}
看到這裡有entityframework 遷移經驗的基本上發現這是出奇的相似啊。
接下來就是建立我們的實體結構,laravel 的結構產生器可以參考 http://v4.golaravel.com/docs/4.1/schema
1 <?php 2 3 use Illuminate\Database\Schema\Blueprint; 4 use Illuminate\Database\Migrations\Migration; 5 6 class CreateTablenameTable extends Migration { 7 8 /** 9 * Run the migrations.10 *11 * @return void12 */13 public function up()14 {15 Schema::create(‘posts‘, function(Blueprint $table) {16 $table->increments(‘id‘);17 $table->unsignedInteger(‘user_id‘);18 $table->string(‘title‘);19 $table->string(‘read_more‘);20 $table->text(‘content‘);21 $table->unsignedInteger(‘comment_count‘);22 $table->timestamps();23 });24 25 Schema::create(‘comments‘, function(Blueprint $table) {26 $table->increments(‘id‘);27 $table->unsignedInteger(‘post_id‘);28 $table->string(‘commenter‘);29 $table->string(‘email‘);30 $table->text(‘comment‘);31 $table->boolean(‘approved‘);32 $table->timestamps();33 });34 35 Schema::table(‘users‘, function (Blueprint $table) {36 $table->create();37 $table->increments(‘id‘);38 $table->string(‘username‘);39 $table->string(‘password‘);40 $table->string(‘email‘);41 $table->string(‘remember_token‘, 100)->nullable();42 $table->timestamps();43 });44 }45 46 /**47 * Reverse the migrations.48 *49 * @return void50 */51 public function down()52 {53 Schema::drop(‘posts‘);54 55 Schema::drop(‘comments‘);56 57 Schema::drop(‘users‘);58 }59 60 }
繼續在上面的命令視窗輸入 php artisan migrate 將執行遷移
更多遷移相關知識:http://v4.golaravel.com/docs/4.1/migrations
先寫到這裡明天繼續
.net轉php laraval架構學習系列(二)項目實戰---Models