:::
7. 開始定義系統各項功能
一、定義路由
- 這個單元先看看就好,不用實作,只是先列出總理,方便參考用。
- 定義各項功能基本上就是定義路由,白話一點就是說,網址輸入什麼,就去執行什麼動作,出現什麼畫面。
- 建議先看一下「簡明RESTful API設計要點」
- 底下的「資源」通常代表一個資料表的意思(當然不盡然如此,只是這樣想比較簡單),例如:exam、user...等。以下寫法範例,「資源」以「exam」為例:
動詞 網址路徑 行為 路由名稱 一般路由寫法 有控制器的路由寫法 GET /資源 index
列表
資源.index Route::get('/exam', function () {
return view('index');
})->name('exam.index');Route::get('/exam', 'ExamController@index')
->name('exam.index');GET /資源/create create
新增
資源.create Route::get('/exam/create', function () {
return view('create');
})->name('exam.create');Route::get('/exam/create', 'ExamController@create')
->name('exam.create');POST
(支援 CSRF保護)
/資源 store
儲存
資源.store Route::post('/exam', function () {
return view('store');
})->name('exam.store');Route::post('/exam/store', 'ExamController@store')
->name('exam.store');GET /資源/{id} show
顯示一筆
資源.show Route::get('/exam/{id}', function () {
return view('show');
})->name('exam.show');Route::get('/exam/{id}', 'ExamController@show')
->name('exam.show');GET /資源/{id}/edit edit
編輯
資源.edit Route::get('/exam/{id}/edit', function () {
return view('edit');
})->name('exam.edit');Route::get('/exam/{id}/edit', 'ExamController@edit')
->name('exam.edit');PUT/PATCH
(支援 CSRF保護)
/資源/{id} update
更新
資源.update Route::patch('/exam/{id}', function () {
return view('update');
})->name('exam.update');Route::patch('/exam/{id}', 'ExamController@update')
->name('exam.update');DELETE
(支援 CSRF保護)
/資源/{id} destroy
刪除
資源.destroy Route::delete('/exam/{id}', function () {
return view('destroy');
})->name('exam.destroy');Route::delete('/exam/{id}', 'ExamController@destroy')
->name('exam.destroy'); - 搭配「動詞」+「路徑」,可以執行不同「行為」
- 其中,盡量用符合的動詞和路徑,「行為」的命名也盡量和上面相同,如此,未來可以簡化許多程式。
- CSRF是跨站請求偽造 (英語:Cross-site request forgery)
6-1 編輯並執行測驗(exam)的 migrate 檔案