【问题标题】:Laravel Route::group[] not working correctlyLaravel Route::group[] 无法正常工作
【发布时间】:2021-04-06 13:08:50
【问题描述】:

我有想按名称、前缀和中间件组合在一起的路由。出于某种原因,Route::group 函数的 'name' 选项没有正确地尾随名称。我的代码不正确,还是这是一个错误?下面是路由组定义。

Route::group(['name' => 'admin.', 'prefix' => 'admin',
    'middleware' => 'admin'], function () {
    Route::get('/', function () {
        return 'the index page';
    })->name('index');

    Route::get('/another', function () {
        return 'another page';
    })->name('another');
});

然后我清除并缓存了路线。这是清单。

+--------+----------+------------------------+-----------------------------+------------------------------------------------------------------------+------------+
| Domain | Method   | URI                    | Name                        | Action                                                                 | Middleware |
+--------+----------+------------------------+-----------------------------+------------------------------------------------------------------------+------------+
|        | GET|HEAD | admin                  | index                       | Closure                                                                | web        |
|        |          |                        |                             |                                                                        | admin      |
|        | GET|HEAD | admin/another          | another                     | Closure                                                                | web        |
|        |          |                        |                             |                                                                        | admin      |

我希望在名称中看到 admin.index、admin.another、...

但是,如果我使用 Route::name 函数,它将正常工作。

Route::name('admin.')->group(function () {
    Route::prefix('admin')->group(function () {
        Route::middleware('admin')->group(function () {
            Route::get('/', function () {
                return 'the index page';
            })->name('index');

            Route::get('/another', function () {
                return 'another page';
            })->name('another');
        });
    });
});

+--------+----------+------------------------+-----------------------------+------------------------------------------------------------------------+---------------+
| Domain | Method   | URI                    | Name                        | Action                                                                 | Middleware    |
+--------+----------+------------------------+-----------------------------+------------------------------------------------------------------------+---------------+
|        | GET|HEAD | admin                  | admin.index                 | Closure                                                                | web           |
|        |          |                        |                             |                                                                        | admin         |
|        | GET|HEAD | admin/another          | admin.another               | Closure                                                                | web           |
|        |          |                        |                             |                                                                        | administrator |

【问题讨论】:

    标签: laravel-routing laravel-8


    【解决方案1】:

    您应该像这样用as 替换name 数组条目:

    Route::group(['as' => 'admin.', 'prefix' => 'admin',
        'middleware' => 'admin'], function () {
        Route::get('/', function () {
            return 'the index page';
        })->name('index');
    
        Route::get('/another', function () {
            return 'another page';
        })->name('another');
    });
    

    但是我相信您的第二种方法更具可读性。但请记住,无需为每个属性定义 group 方法,您可以简单地将所有方法链接起来,并在最后定义单个 group 方法:

    Route::name('admin.')
        ->prefix('admin')
        ->middleware('admin')
        ->group(function () {
            Route::get('/', function () {
                return 'the index page';
            })->name('index');
    
            Route::get('/another', function () {
                return 'another page';
            })->name('another');
        });
    
    

    【讨论】:

    • 我真的很喜欢这个。谢谢!
    猜你喜欢
    • 2021-08-13
    • 2021-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-17
    相关资源
    最近更新 更多