Laravel 5.3错误处理从中止()

问题描述:

我试图处理与laravel 5.3自定义错误,但似乎无法得到正确捕获中止(404)。Laravel 5.3错误处理从中止()

如果我放弃我的路线(web.php)我得到我的404页面。

如果我把中止在刀刃上,我得到一个NotFoundHttpException消息,但默认为我的后备错误条件,一个500

应用/异常/ handler.php渲染功能:

// 404 page when a model is not found 
    if ($exception instanceof ModelNotFoundException or $exception instanceof NotFoundHttpException) { 
     return response()->view('errors.404', $requestAndException, 404); 
    } 

    if ($this->isHttpException($exception)) { 
     //return $this->renderHttpException($exception); 
     // mirrored code from renderHttpException to allow for exception-less errors in non-dev mode 
     $status = $exception->getStatusCode(); 

     if (view()->exists("errors.{$status}")) { 
      return response()->view("errors.{$status}", $requestAndException, $status, $exception->getHeaders()); 
     } else { 
      return $this->convertExceptionToResponse($exception); 
     } 
    } else { 
     // Custom error 500 view on production 
     return response()->view('errors.500', $requestAndException, 500); 
    } 

我试着用特定的例外更新使用部分,但似乎没有什么区别。目前坐落于:

use Exception; 
use Illuminate\Auth\AuthenticationException; 
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; 

任何想法?

在你的views文件夹中创建一个名为“errors”的文件夹,然后在里面创建你自定义的例子视图:404.blade.php,500.blade.php。 当你以视图名称的相同状态中止时,Laravel会为你处理它,所以你不必放入你的路线。

+0

我确实有。这是行不通的。当我在视图内呼叫放弃时。 “ ”如果我放弃我的路线(web.php)我得到我的404页 如果我放弃刀片,我得到一个NotFoundHttpException消息,但默认为我的回退错误条件,500。 “ –

+0

你不需要把它放在你的路线上,你只需要在错误文件夹中用正确的名字创建视图,laravel会照顾到你的一切。 –

+0

然后放入控制器或中间件。 –