Laravel 項目開發(fā)規(guī)范 路由器

2023-02-16 17:12 更新

路由閉包

絕不 在路由配置文件里書寫『閉包路由』或者其他業(yè)務(wù)邏輯代碼,因為一旦使用將無法使用 路由緩存 。

路由器要保持干凈整潔,絕不 放置除路由配置以外的其他程序邏輯。

控制器方法定義

路由中的控制器方法定義,必須 使用 Controller::class 這種方式加載。

? 正確

Route::get('/photos', [PhotosController::class, 'index'])->name('photos.index');

? 錯誤的例子:

Route::get('/photos', 'PhotosController@index')->name('photos.index');

這樣做 IDE 可以加代碼索引。有兩個好處:

  1. 支持點擊跳轉(zhuǎn)到方法;
  2. 支持重構(gòu)。

Restful 路由

必須 優(yōu)先使用 Restful 路由,配合資源控制器使用。


超出 Restful 路由的,應(yīng)該 模仿上圖的方式來定義路由。

resource 方法正確使用

一般資源路由定義:

Route::resource('photos', PhotosController::class);

等于以下路由定義:

Route::get('/photos', [PhotosController::class, 'index'])->name('photos.index');
Route::get('/photos/create', [PhotosController::class, 'create'])->name('photos.create');
Route::post('/photos', [PhotosController::class, 'store'])->name('photos.store');
Route::get('/photos/{photo}', [PhotosController::class, 'show'])->name('photos.show');
Route::get('/photos/{photo}/edit', [PhotosController::class, 'edit'])->name('photos.edit');
Route::put('/photos/{photo}', [PhotosController::class, 'update'])->name('photos.update');
Route::delete('/photos/{photo}', [PhotosController::class, 'destroy'])->name('photos.destroy');

使用 resource 方法時,如果僅使用到部分路由,必須 使用 only 列出所有可用路由:

Route::resource('photos', PhotosController::class, ['only' => ['index', 'show']]);

絕不 使用 except,因為 only 相當(dāng)于白名單,相對于 except 更加直觀。路由使用白名單有利于養(yǎng)成『安全習(xí)慣』。

單數(shù) or 復(fù)數(shù)?

資源路由路由 URI 必須 使用復(fù)數(shù)形式,如:

  • /photos/create
  • /photos/{photo}

錯誤的例子如:

  • /photo/create
  • /photo/{photo}

路由模型綁定

在允許使用路由 模型綁定 的地方 必須 使用。

模型綁定代碼 必須 放置于 app/Providers/RouteServiceProvider.php 文件的 boot 方法中:

    public function boot()
    {
        Route::bind('user_name', function ($value) {
            return User::where('name', $value)->first();
        });

        Route::bind('photo', function ($value) {
            return Photo::find($value);
        });

        parent::boot();
    }

注:如果使用了 ?{id}? 作為參數(shù),Laravel 已經(jīng)默認做了綁定。

全局路由器參數(shù)

出于安全考慮,應(yīng)該 使用全局路由器參數(shù)限制。

必須 在 RouteServiceProvider 文件的 boot 方法里定義模式:

/**
 * 定義你的路由模型綁定, pattern 過濾器等。
 *
 * @return void
 */
public function boot()
{
    Route::pattern('id', '[0-9]+');

    parent::boot();
}

模式一旦被定義,便會自動應(yīng)用到所有使用該參數(shù)名稱的路由上:

Route::get('users/{id}', [UsersController::class, 'show']);
Route::get('photos/{id}', [PhotosController::class, 'show']);

只有在 id 為數(shù)字時,才會路由到控制器方法中,否則 404 錯誤。

路由命名

除了 resource 資源路由以外,其他所有路由都 必須 使用 name 方法進行命名。

必須 使用『資源前綴』作為命名規(guī)范,如下的 users.follow,資源前綴的值是 users.

Route::post('users/{id}/follow', [UsersController::class, 'follow'])->name('users.follow');

獲取 URL

獲取 URL 必須 遵循以下優(yōu)先級:

  1. $model->link()
  2. route 方法
  3. url 方法

在 Model 中創(chuàng)建 link() 方法:

public function link($params = [])
{
    $params = array_merge([$this->id], $params);
    return route('models.show', $params);
}

所有單個模型數(shù)據(jù)鏈接使用:

$model->link();

// 或者添加參數(shù)
$model->link($params = ['source' => 'list'])

『單個模型 URI』經(jīng)常會發(fā)生變化,這樣做將會讓程序更加靈活。

除了『單個模型 URI』,其他路由 必須 使用 route 來獲取 URL:

$url = route('profile', ['id' => 1]);

無法使用 route 的情況下,可以 使用 url 方法來獲取 URL:

url('profile', [1]);


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號