1

我正在学习 Laravel,我想使用前缀对一些路由进行分组并命名每条路由,所以我得到了这样的东西

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');
});

我想避免写“客户”。在每个路线名称中。

我尝试过使用名称,但它无法识别路由名称前缀“客户”。

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');
});

我发现的另一种方式是使用asuse,但它似乎有很多代码,而且看起来不太干净,实际上第一种方式看起来更干净。

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']);
});

有更好的方法吗?

4

3 回答 3

3

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

于 2020-08-14T09:35:53.113 回答
2

您可以->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        |
于 2020-08-14T09:37:30.780 回答
2

为什么不使用资源

您只需执行以下操作即可通过一行获得所需的结果:

Route::resource('customers', 'CustomerController')->only(['index', 'create', 'store']);

路线:

+----------+-------------------------------+
| Method   | URI                           |
+----------+-------------------------------+
| GET|HEAD | customers                     |
| POST     | customers                     |
| GET|HEAD | customers/create              |
+----------+-------------------------------+
于 2020-08-14T09:37:38.453 回答