:::

7. 開始定義系統各項功能

一、定義路由

  1. 這個單元先看看就好,不用實作,只是先列出總理,方便參考用。
  2. 定義各項功能基本上就是定義路由,白話一點就是說,網址輸入什麼,就去執行什麼動作,出現什麼畫面
  3. 建議先看一下「簡明RESTful API設計要點
  4. 底下的「資源」通常代表一個資料表的意思(當然不盡然如此,只是這樣想比較簡單),例如: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');
  5. 搭配「動詞」+「路徑」,可以執行不同「行為」
  6. 其中,盡量用符合的動詞和路徑,「行為」的命名也盡量和上面相同,如此,未來可以簡化許多程式。
  7. CSRF是跨站請求偽造英語:Cross-site request forgery

:::

書籍目錄

展開 | 闔起

https%3A%2F%2Fcampus-xoops.tn.edu.tw%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbdsn%3D1160

計數器

今天: 3691369136913691
昨天: 3438343834383438
總計: 7394020739402073940207394020739402073940207394020