【问题标题】:404 Error Handling in Laravel 5.2.15Laravel 5.2.15 中的 404 错误处理
【发布时间】:2016-06-08 15:39:03
【问题描述】:

我问这个问题是因为在这个问题中添加我的评论后我没有得到回复

laravel routing and 404 error

在上面的答案中,我们可以看到下面的代码用于filters.php

App::missing(function($exception)
{
    return Response::view('errors.missing', array(), 404);
});

但是,我认为我们在最新版本中没有filters.php。有人可以提出更好的方法来处理 404 错误吗?

【问题讨论】:

    标签: php laravel-5.1 laravel-5.2


    【解决方案1】:

    您不再需要这样做了。不要包括那个。你所做的就是在你的 resources/views/errors 文件夹中放置一个名为 404.blade.php 的视图文件(你的 404 错误视图),Laravel 将为你处理 404 错误。

    【讨论】:

      【解决方案2】:

      看看 http://www.jeffmould.com/2016/05/25/laravel-5-error-handling/

      我只是更改这一行 App/Exceptions/Handler.php 文件。

      public function render($request, Exception $e)
          {
              // the below code is for Whoops support. Since Whoops can open some security holes we want to only have it
              // enabled in the debug environment. We also don't want Whoops to handle 404 and Validation related exceptions.
              if (config('app.debug') && !($e instanceof ValidationException) && !($e instanceof HttpResponseException))
              {
      
       /******************here I changed**********************/
      
      
                 # return $this->renderExceptionWithWhoops($e);
                 return response()->view('errors.404', [], 404);
              }
      
      
              // this line allows you to redirect to a route or even back to the current page if there is a CSRF Token Mismatch
              if($e instanceof TokenMismatchException){
                  return redirect()->route('index');
              }       
      
              // let's add some support if a Model is not found 
              // for example, if you were to run a query for User #10000 and that user didn't exist we can return a 404 error
              if ($e instanceof ModelNotFoundException) {
                  return response()->view('errors.404', [], 404);
              }  
      
              // Let's return a default error page instead of the ugly Laravel error page when we have fatal exceptions
              if($e instanceof \Symfony\Component\Debug\Exception\FatalErrorException) {
                  return \Response::view('errors.500',array(),500);
              }
      
              // finally we are back to the original default error handling provided by Laravel
              if($this->isHttpException($e))
              {
                  switch ($e->getStatusCode()) {
                      // not found
                      case 404:
                          return \Response::view('errors.404',array(),404);
                      break;
                      // internal error
                      case 500:
                          return \Response::view('errors.500',array(),500);   
                      break;
      
                      default:
                          return $this->renderHttpException($e);
                      break;
                  }
              }
              else
              {
                  return parent::render($request, $e);
              }      
       /******************here I changed**********************/
      
              #return parent::render($request, $e);
          }
      
           if (config('app.debug') && !($e instanceof ValidationException) && !($e instanceof HttpResponseException))
                  {
      

      【讨论】:

        猜你喜欢
        • 2013-12-08
        • 1970-01-01
        • 1970-01-01
        • 2019-08-11
        • 2015-10-31
        • 2015-04-07
        • 2017-03-02
        • 2013-11-24
        • 1970-01-01
        相关资源
        最近更新 更多