laravel MethodNotAllowedHttpException重定向到404

问题描述

我使用laravel 8尝试编辑我的exceptions \ handler.PHP

public function render($request,Throwable $exception)
{
    if ($exception instanceof MethodNotAllowedHttpException) {
        abort(404);
    }
    
    return parent::render($request,$exception);
}

但是当检查MethodMethodAlAlededHttpException的路由时,他给出的不是404,而是500。

解决方法

一种可能的解决方案是为您的 routes/web.php 文件提供路由回退。尝试将以下内容添加到您的网络路由底部:

Route::fallback( function () {
    abort( 404 );
} );