使用Route macro來定義Route的新方法

使用 Laravel 在作後臺表單操做時,一般會增長一個批量刪除的功能 起初是在 route 裏定義一個新的 deleteapi

Route::delete('prefix/destroy-selection', 'CurrentController@destroySelection); 複製代碼

以後再定義 apiResourcebash

Route::apiResource('prefix', 'CurrentController');
複製代碼

這樣寫十分囉嗦且冗長,Laravel 的強大之處就是它提供了 Macro 來擴展功能ide

在 AppServiceProvider 的 boot 方法里加入spa

Route::macro('full',function ($prefix, $controller){
            Route::delete($prefix.'/destroy-selection', $controller.'@destroySelection')->name($prefix.'destroy-selection');
            Route::apiResource($prefix, $controller);
});
複製代碼

以後在 route 裏就能夠直接使用 full 來定義了code

Route::full('prefix', 'CurrentController');
複製代碼
相關文章
相關標籤/搜索