Laravel 7.x 命名路由组中的路由
Laravel 7.x name routes in routes group
我正在学习 Laravel 并且我想使用 prefix 对一些路线进行分组并命名每条路线所以我得到了这样的东西
Route::prefix('customers')->group(
function(){
Route::get('/', 'CustomerController@index')->name('customers.index');
Route::get('/create', 'CustomerController@create')->name('customers.create');
Route::post('/', 'CustomerController@store')->name('customers.store');
});
我想避免在每个路由名称中写 'customers.'。
我尝试使用 group 和 name 但它无法识别路由名称前缀 'customers.'
Route::group(['prefix' => 'customers', 'name' => 'customers.'],
function(){
Route::get('/', 'CustomerController@index')->name('index');
Route::get('/create', 'CustomerController@create')->name('create');
Route::post('/', 'CustomerController@store')->name('store');
});
我发现的另一种方法是使用 as 和 use 但它似乎代码太多,而且看起来不太干净,实际上第一个看起来更干净。
Route::group([
'prefix' => 'customers',
'as' => 'customers.'
],
function(){
Route::get('/', ['as'=>'index', 'uses' => 'CustomerController@index']);
Route::get('/create', ['as'=>'create', 'uses' => 'CustomerController@create']);
Route::post('/', ['as'=>'store', 'uses' => 'CustomerController@store']);
});
有更好的方法吗?
RESTful 资源控制器
RESTful 资源控制器为您设置一些默认路由,甚至为它们命名。
Route::resource('users', 'UsersController');
为您提供这些命名路线:
Verb Path Action Route Name
GET /users index users.index
GET /users/create create users.create
POST /users store users.store
GET /users/{user} show users.show
GET /users/{user}/edit edit users.edit
PUT|PATCH /users/{user} update users.update
DELETE /users/{user} destroy users.destroy
你会像这样设置你的控制器(动作=方法)
class UsersController extends BaseController {
public function index() {}
public function show($id) {}
public function store() {}
}
您还可以像这样选择包含或排除哪些操作:
Route::resource('users', 'UsersController', [
'only' => ['index', 'show']
]);
Route::resource('monkeys', 'MonkeysController', [
'except' => ['edit', 'create']
]);
取自Laravel - Route::resource vs Route::controller
分组时可以只用->name(XYZ)
:
Route::prefix('customers')
->name('customers.')
->group(function () {
Route::get('/', 'CustomerController@index')->name('index');
Route::get('/create', 'CustomerController@create')->name('create');
Route::post('/', 'CustomerController@store')->name('store');
});
路线:
| | POST | customers | customers.store | App\Http\Controllers\CustomerController@store | web |
| | GET|HEAD | customers | customers.index | App\Http\Controllers\CustomerController@index | web |
| | GET|HEAD | customers/create | customers.create | App\Http\Controllers\CustomerController@create | web |
为什么不使用 resource?
您只需执行以下操作即可在一行中获得您想要的结果:
Route::resource('customers', 'CustomerController')->only(['index', 'create', 'store']);
路线:
+----------+-------------------------------+
| Method | URI |
+----------+-------------------------------+
| GET|HEAD | customers |
| POST | customers |
| GET|HEAD | customers/create |
+----------+-------------------------------+
我正在学习 Laravel 并且我想使用 prefix 对一些路线进行分组并命名每条路线所以我得到了这样的东西
Route::prefix('customers')->group(
function(){
Route::get('/', 'CustomerController@index')->name('customers.index');
Route::get('/create', 'CustomerController@create')->name('customers.create');
Route::post('/', 'CustomerController@store')->name('customers.store');
});
我想避免在每个路由名称中写 'customers.'。
我尝试使用 group 和 name 但它无法识别路由名称前缀 'customers.'
Route::group(['prefix' => 'customers', 'name' => 'customers.'],
function(){
Route::get('/', 'CustomerController@index')->name('index');
Route::get('/create', 'CustomerController@create')->name('create');
Route::post('/', 'CustomerController@store')->name('store');
});
我发现的另一种方法是使用 as 和 use 但它似乎代码太多,而且看起来不太干净,实际上第一个看起来更干净。
Route::group([
'prefix' => 'customers',
'as' => 'customers.'
],
function(){
Route::get('/', ['as'=>'index', 'uses' => 'CustomerController@index']);
Route::get('/create', ['as'=>'create', 'uses' => 'CustomerController@create']);
Route::post('/', ['as'=>'store', 'uses' => 'CustomerController@store']);
});
有更好的方法吗?
RESTful 资源控制器
RESTful 资源控制器为您设置一些默认路由,甚至为它们命名。
Route::resource('users', 'UsersController');
为您提供这些命名路线:
Verb Path Action Route Name
GET /users index users.index
GET /users/create create users.create
POST /users store users.store
GET /users/{user} show users.show
GET /users/{user}/edit edit users.edit
PUT|PATCH /users/{user} update users.update
DELETE /users/{user} destroy users.destroy
你会像这样设置你的控制器(动作=方法)
class UsersController extends BaseController {
public function index() {}
public function show($id) {}
public function store() {}
}
您还可以像这样选择包含或排除哪些操作:
Route::resource('users', 'UsersController', [
'only' => ['index', 'show']
]);
Route::resource('monkeys', 'MonkeysController', [
'except' => ['edit', 'create']
]);
取自Laravel - Route::resource vs Route::controller
分组时可以只用->name(XYZ)
:
Route::prefix('customers')
->name('customers.')
->group(function () {
Route::get('/', 'CustomerController@index')->name('index');
Route::get('/create', 'CustomerController@create')->name('create');
Route::post('/', 'CustomerController@store')->name('store');
});
路线:
| | POST | customers | customers.store | App\Http\Controllers\CustomerController@store | web |
| | GET|HEAD | customers | customers.index | App\Http\Controllers\CustomerController@index | web |
| | GET|HEAD | customers/create | customers.create | App\Http\Controllers\CustomerController@create | web |
为什么不使用 resource?
您只需执行以下操作即可在一行中获得您想要的结果:
Route::resource('customers', 'CustomerController')->only(['index', 'create', 'store']);
路线:
+----------+-------------------------------+
| Method | URI |
+----------+-------------------------------+
| GET|HEAD | customers |
| POST | customers |
| GET|HEAD | customers/create |
+----------+-------------------------------+