Skip to content

Instantly share code, notes, and snippets.

@LeoOnTheEarth
Last active August 29, 2015 14:10
Show Gist options
  • Save LeoOnTheEarth/af1806c918e6c2f449aa to your computer and use it in GitHub Desktop.
Save LeoOnTheEarth/af1806c918e6c2f449aa to your computer and use it in GitHub Desktop.

Laravel 4.2

安裝 Laravel Installer

composer global require "laravel/installer=~1.1"

連結 Laravel Installer 執行檔

# In Linux
ln -s /usr/bin/laravel ~/.composer/vendor/bin/laravel
# In OSX
ln -s ~/.composer/vendor/bin/laravel /usr/bin/laravel

產生專案 blog

laravel new blog

安裝 Laravel IDE Helper 來提供 IDE 的支援

使用 composer 安裝 laravel-ide-helper

composer require barryvdh/laravel-ide-helper *

修改 app/config/app.php 新增 ServiceProvider "Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider"

執行以下指令

php artisan ide-helper:generate

安裝 PHPStorm Laravel Plugins

  • 勾選 Enable plugin for this project

修改設定檔

  • 設定檔位置 app/config
    在此資料夾下的設定檔預設是 for production 使用
    其他的環境的設定檔要在底下開子資料夾後,複寫 production 的設定
  • 資料庫設定 app/config/local/database.php

Database Migration

php artisan migrate:make create_users_table
public function up()
{
	Schema::create('users', function(Blueprint $table)
	{
		$table->increments('id');
		$table->string('username', 16);
		$table->string('password', 64);
		$table->string('email', 128);
		$table->string('remember_token', 60);
		$table->timestamps();
	});
}

public function down()
{
	Schema::drop('users');
}
php artisan migrate:make blog
public function up()
{
	Schema::create('categories', function(Blueprint $table)
	{
		$table->increments('id');
		$table->string('title', 128);
		$table->string('description', 256);
		$table->timestamp('created_at');
		$table->unsignedInteger('created_by');
		$table->timestamp('updated_at');
		$table->unsignedInteger('updated_by');
	});

	Schema::create('posts', function(Blueprint $table)
	{
		$table->increments('id');
		$table->string('title', 128);
		$table->text('content');
		$table->unsignedInteger('category_id');
		$table->timestamp('created_at');
		$table->unsignedInteger('created_by');
		$table->timestamp('updated_at');
		$table->unsignedInteger('updated_by');
	});
}

public function down()
{
	Schema::drop('posts');
	Schema::drop('categories');
}

產生假資料

// app/database/seeds/UserTableSeeder.php

class UserTableSeeder extends Seeder
{
	public function run()
	{
		DB::table('users')->delete();

		User::create(
			array(
				'username' => 'smstw',
				'password' => Hash::make('1234'),
				'email' => 'smstw@bm-sms.com.tw',
				'remember_token' => '',
			)
		);
	}
}
// app/database/seeds/CategoryTableSeeder.php

class CategoryTableSeeder extends Seeder
{
	public function run()
	{
		DB::table('categories')->delete();

		Category::create(
			array(
				'title' => '雜文',
				'description' => '有的沒的文章',
				'created_by' => 1,
				'updated_by' => 1,
			)
		);

		Category::create(
			array(
				'title' => '生活',
				'description' => '生活瑣事',
				'created_by' => 1,
				'updated_by' => 1,
			)
		);
	}
}
// app/database/seeds/PostTableSeeder.php

class PostTableSeeder extends Seeder
{
	public function run()
	{
		DB::table('posts')->delete();

		Post::create(
			array(
				'title' => '四公小個痛商會太通去命海',
				'content' => '
經候根我故石城之月國士自鄉靈死多我家水不,還來色花女處值告消天引他小所的也爭立多去稱時不入失車;看果也最人又、玩花著你表買,子長品味意大花文雙性支到聽;完家斯與全不分際成投來文上例的;於全格入友男痛利以……法法做女力跑人帶片地得列要條素此合字行望首,兩身需近電母器但意代常花大排成之減易常委水全不邊長對民減投道親包名又道血重也生史海了候得法以然……照分半好禮年情同部間去大!種低口樣花必爭個賽生,代公筆情知界把要,的相後片!談跑自調果德中,年善隊子為領夫自林訴:把西急是創決響外香管出備入權一。正談已坐而功,量車她的小民合藝意草今場候特交夠造神球……子縣不研有士已面。

告何覺道支呢岸站品著,著分早模們苦那友有,公會近收代問立角環。有還健明世,問大分、標給斷呢的中公己持消為除組企記不是才界低政了社前。

性提停子總進味業是一眼海國西!體人看庭有力初情媽長有!

組實可父極司。的收由年都出他完臺其好善望經因性是?
',
				'category_id' => 1,
				'created_by' => 1,
				'updated_by' => 1,
			)
		);

		Post::create(
			array(
				'title' => '問業大明少民住我的',
				'content' => '
且氣人要一未神過,人解看工市母器華樂位減國家一各思……北成了須?發近們認,部平同十中樹轉大算排了是有呢著多上來進香以會,希李中現到我辦要女細舞開,我兒克態著善臺不雲我熱裡,而感來教上我每華低,一興深然子港工細可中的……洲死開談月,響易的爭解。活人品作車化把化讀構我出……我產的生色。財房數?製還有痛以那機的她古所請……兩有維況不;方屋道演力認子見有解女充自?麗現軍經隊進名歡本都學時熱等手德提身以高食方,中汽緊賽原和!走表覺時間夠哥面因手從破學個。

器手他裡愛教本積設功野加這過;不排動消合興失成、多大多從裡爾。

別長人境市史和,大就全高音非痛包活票陸把像界電,林文過話交朋實接這為別聽黨其立視不本地象。爸女你家我力世可己魚:本人年年可象此詩突電,家是?

念我創絕維沒我等因情是……有升初出生配。及我現列出而圖天前轉學臺事他國心說天好開了兒,灣王引一都們,病信教健防道的計……向以的不一孩主開麼聽後個子的師資?小足王克間後生果如影況一我一招裡重李。
',
				'category_id' => 1,
				'created_by' => 1,
				'updated_by' => 1,
			)
		);

		Post::create(
			array(
				'title' => '太運燈時動保坡禮經內',
				'content' => '
子相製布施部來無同長遊說麼比時上笑著有都定形能山決非能是;價個童禮花。現民。

知賣配……去痛細雨持間試時不體。

上男前為不兒使是修地下、世備那師集什一明面到,說大原少後知要知班中面、石的專日國術酒關樹會活教有苦易出濟,些知外作印我我半。精老現到他。一功電管性製力媽過濟最部張次是古邊力以,壓構成樣新。傷內自裝論的然新輪錢不寶清個理,林書已著地爸主花奇兒營一制。明一去術信似,斯最高簡大百常決;機時有著你每中……離應了設有完失的年子條日世自了灣社香高部光,廣以之,可新要市國的多的人車空望媽,任是即,我完路見下華病費個件子,病你坡思離角斷證身食夜美車太西到說。軍人質醫不利這大發不:消因至家還內中食示!

就的紅人;司車後己出對子多也花就建開次到獨求太供大大東幾從麗陸:負什公象個。他具式有跟李銷童務面事理統孩為隨病上此可石覺省作女野能心風人大動,就上情招等腳:門賽得回國血回當直經是下前,個都調登下康張,一其後通最府家的爸真華如提條說中前。
',
				'category_id' => 2,
				'created_by' => 1,
				'updated_by' => 1,
			)
		);
	}
}

執行下面的指令產生 schema 和假資料

php artisan migrate --env=local --seed

編輯 Routes

我們需要以下幾個頁面

Route Pattern 說明
/ 首頁,顯示 5 篇文章與上一頁 / 下一頁按鈕
/post/{id}/{title} 單篇文章頁面
/category/{id}/{title} 分類頁面,顯示該分類的 5 篇文章與上一頁 / 下一頁按鈕
/post/create /post/edit/{id}/{title} /post/save 新增 / 編輯文章頁面
/post/delete/{id} 刪除文章
/category/create /category/edit/{id}/{title} /category/save 新增 / 編輯分類頁面
/category/delete/{id} 刪除分類頁面
/user/login /user/login/check 登入頁面
/user/logout 登出
/user/profile 帳號資訊頁面
/user/profile/edit /user/profile/save 編輯帳號頁面
/user/register /user/register/save 帳號註冊頁面
/user/register/success 帳號註冊完成頁面

總共 20 個 action

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment