简体   繁体   English

Laravel 8 中未定义命名路由

[英]Named route is not defined in Laravel 8

Instead of redirecting, I'm receiving the error below.我收到以下错误,而不是重定向。 Why?为什么?

Route [moderator.products] not defined.路线 [moderator.products] 未定义。

These are my routes in a middleware's group function:这些是我在中间件组 function 中的路由:

Route::get('moderator/products', [ModeratorController::class, 'products'])
    ->name('moderator.products');
Route::redirect('/', route('moderator.products'));

The issue can be resolved by naming the route first without chaining it at the end.这个问题可以通过先命名路由而不在最后链接它来解决。


Route::name('moderator.products')->get('moderator/products', [ModeratorController::class, 'products']);

Route::redirect('/', route('moderator.products'));

I tested them.我测试了它们。 It reidrects to the mentioned named route.它重定向到提到的命名路线。 All you need to do is to first call the name() then chain the get() .您需要做的就是首先调用name()然后链接get()

Not an answer, but I got around this by not using the named route, and using the url, ie swap route('moderator.products') for url('moderator/products') .不是答案,但我通过不使用命名路由并使用 url 解决了这个问题,即交换route('moderator.products')url('moderator/products')

EDIT: I did a bunch of code editing to get past my problem, so I'm not sure, but I suspect my issue was caused by naming the exact same route twice.编辑:我做了一堆代码编辑来解决我的问题,所以我不确定,但我怀疑我的问题是由两次命名完全相同的路线引起的。 Double check to make sure you haven't done this if you get this problem.如果遇到此问题,请仔细检查以确保您没有这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM