【问题标题】:Laravel 5.6 - how to implement 404 page/routeLaravel 5.6 - 如何实现 404 页面/路由
【发布时间】:2018-12-18 07:54:52
【问题描述】:

我正在尝试实现 404 页面,但到目前为止什么都没有发生。我得到了这个:

Not Found

The requested URL /test was not found on this server.

我有自定义的 404 页面,其中的文字完全不同。

在我的 routes 文件中,我有这条路线:

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 位于 resources/view/errors

【问题讨论】:

  • 视图中文件夹的分隔符是点而不是斜线。试试return response()->view('errors.404', [], 404);
  • 也许这个答案? stackoverflow.com/a/51256791/3585500
  • 你也可以只使用中止助手laravel.com/docs/5.6/helpers#method-abort,它默认为你使用的视图。
  • 这看起来像默认的服务器 404 错误页面,即请求实际上并没有命中 Laravel。所以你可能没有正确配置 URL 重写。

标签: laravel laravel-5.6


【解决方案1】:

404.blade.php 文件应位于 resources/views/errors 下(注意视图中的“s”)。而且你不需要在你的路由和 Handler.php 文件中自定义代码,Laravel 可以自己处理 404。

【讨论】:

    【解决方案2】:

    错误信息

    Not Found
    
    The requested URL /test was not found on this server.
    

    是默认的服务器 404 错误消息,而不是来自 Laravel。

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

    你可以在how to enable mod_rewrite on apache查看这篇文章

    【讨论】:

      【解决方案3】:

      在 handler.php 中导入

      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 页面的方式。 TokenMismatchException 是会话过期,状态码 422 是验证错误。这里 $request->expectsJson() 用于 ajax json 进程

      【讨论】:

        猜你喜欢
        • 2019-03-31
        • 2018-09-26
        • 2015-01-02
        • 1970-01-01
        • 2018-12-19
        • 1970-01-01
        • 2016-10-20
        • 1970-01-01
        • 2019-07-10
        相关资源
        最近更新 更多