如果请求不正确,则重定向到路由

Redirect to a route if the request is not correct

我的问题是,如果用户尝试使用 'web' 路由中未定义的请求方法访问页面,则重定向用户。目前这会引发 Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException 错误:

The GET method is not supported for this route. Supported methods: POST.

您可以使用如下的路由定义


Route::any('/{any}', function(Request $request) {

    return redirect('/'); //or any other route preferred
})
->where('any', '.*');

将此定义放在所有路由的末尾。

您可以利用回退路由功能

 Route::fallback(function ()
{
    return redirect()->route('home'); // or redirect()->url('home')
});

请注意,备用路由应始终是您的应用程序注册的最后一条路由 https://laravel.com/docs/8.x/routing#fallback-routes