Laravel中出现Unable to prepare route [home] for serialization. Another route has already been assigned name [home]

Laravel框架
571
0
0
2022-05-03

今天在执行laravel生成route缓存命令时,php artisan route:cache, 出现这个错误:

Route cache cleared!

   LogicException 

 Unable to prepare route [home] for serialization. Another route has already been assigned name [home].

怎么修复呢,其实就是有两个路由定义了同一个规则,给另一个路由改个名字即可。

Route::get('/', [HomeController::class, 'index'])->name('home');
Route::post('/', [HomeController::class, 'index'])->name('home');

把后面这条路由改个名字,

Route::post('/', [HomeController::class, 'index'])->name('home.post');

其他路由也是同理

不过还有一种情况也会出现这个错误,我直接举个例子清楚一点

$router->resource('cms/setting', 'Cms\SettingController');
$router->resource('tool/setting', 'Tool\SettingController');

怎么解决呢,加个选项改个路由名就可以了

$router->resource('cms/setting', 'Cms\SettingController', ['as'=>'cms]);
$router->resource('tool/setting', 'Tool\SettingController', ['as'=>'tool']);