Laravel 5.6-如何实施404页面/路线

Laravel 5.6-如何实施404页面/路线,laravel,laravel-5.6,Laravel,Laravel 5.6,我试图实现404页,但到目前为止什么都没有发生。我明白了: Not Found The requested URL /test was not found on this server. 我有一个完全不同文本的自定义404页面 在我的路由文件中,我有以下路由: Route::fallback(function(){ return response()->view('errors/404', [], 404); }); 在Handler.php中,我添加了以下内容: /**

我试图实现404页,但到目前为止什么都没有发生。我明白了:

Not Found

The requested URL /test was not found on this server.
我有一个完全不同文本的自定义404页面

在我的路由文件中,我有以下路由:

Route::fallback(function(){
    return response()->view('errors/404', [], 404);
});
Handler.php中,我添加了以下内容:

/**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $exception)
    {
        if ($exception instanceof MethodNotAllowedHttpException)
            abort(404);

        if ($this->isHttpException($exception)) {
            if ($exception->getStatusCode() == 404) {
                return response()->view('errors.404', [], 404);
            }
        }


        return parent::render($request, $exception);
    }

404.blade.php位于资源/view/errors

404.blade.php文件应位于资源/view/errors下(注意视图中的“s”)。您不需要在routes和Handler.php文件中使用自定义代码,Laravel可以自己处理404。

错误消息

Not Found

The requested URL /test was not found on this server.
是默认的服务器404错误消息,而不是来自Laravel

当您不配置自定义错误页面时,应该会看到Laravel的默认错误页面。 这意味着您可能没有在服务器上正确配置“重写”,并且Laravel没有收到请求

您可以在handler.php immport中的

上查看这篇文章

use Illuminate\Session\TokenMismatchException;
并将render()函数编辑为

public function render($request, Exception $exception)
    {
        if ($exception instanceof TokenMismatchException) {
            if ($request->expectsJson()) {
                return response()->json([
                    'dismiss' => __('Session expired due to inactivity. Please reload page'),
                ]);
            }
            else{
                return redirect()->back()->with(['dismiss'=>__('Session expired due to inactivity. Please try again')]);
            }
        }
        elseif($exception->getStatusCode()!=422){
            return response()->view('errors.404');
        }
        return parent::render($request, $exception);
    }
这是你将如何重定向到404页上的任何错误。
令牌不匹配异常为会话到期,状态代码422为验证错误。此处$request->expectsJson()用于ajax json进程

视图中文件夹的分隔符是点而不是斜杠。尝试
returnresponse()->view('errors.404',[],404)