2014-10-29 38 views

回答

87

在Laravel 5中,您可以通过编辑app/Exceptions/Handler.php中的render方法来捕捉异常。

如果您想查找缺少的页面(也称为NotFoundException),您需要检查例外$e是否为Symfony\Component\HttpKernel\Exception\NotFoundHttpException的实例。

public function render($request, Exception $e) { 
    if ($e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException) 
     return response(view('error.404'), 404); 

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

通过上面的代码中,我们检查,如果$eSymfony\Component\HttpKernel\Exception\NotFoundHttpException一个instanceof如果是我们发responseview文件error.404作为与HTTP status code 404内容。

这可以用于任何异常。因此,如果您的应用发送了App\Exceptions\MyOwnException的例外情况,那么请检查该实例。

public function render($request, Exception $e) { 
    if ($e instanceof \App\Exceptions\MyOwnException) 
     return ''; // Do something if this exception is thrown here. 

    if ($e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException) 
     return response(view('error.404'), 404); 

    return parent::render($request, $e); 
} 
+4

你怎么知道这个答案,我在哪里可以找到关于此的更多信息? – 2015-02-18 07:00:21

+0

@adityamenon,它是Laravel 5以前的开发版本。我已经重写它以匹配当前版本的Laravel 5. – Marwelln 2015-02-18 10:29:04

+2

Aditya Menon,Handler.php的基础知识和render()函数在文档http://laravel.com/docs/5.0/errors(但Marwelln的回答是如何使用它的一个很好的例子)。 – orrd 2015-02-27 05:56:11

4

由于提交30681dc9acf685missing()方法已经被转移到Illuminate\Exception\Handler类。

所以,一段时间后,你可以这样做:

app('exception')->missing(function($exception) 
{ 
    return Response::view('errors.missing', [], 404); 
}); 


......但最近,一个重构已62ae860完成。现在

,你所能做的就是添加以下到app/Http/Kernel.php

// add this... 
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; 
use Response; 

class Kernel extends HttpKernel { 

    (...) 

    public function handle($request) 
    { 
     try 
     { 
      return parent::handle($request); 
     } 

     // ... and this: 
     catch (NotFoundHttpException $e) 
     { 
      return Response::view('errors.missing', [], 404); 
     } 

     catch (Exception $e) 
     { 
      throw $e; 
     } 
    } 


最后,请记住Laravel仍处于重发展,可能会再次发生变化。

+0

我希望不会马上改变! – 2015-02-18 07:06:01

15

从今天开始提交(9db97c3),您只需要在/ resources/views/errors /文件夹中添加一个404.blade.php文件,它会自动查找并显示它。

+1

不,对我来说不起作用 – 2015-01-02 12:54:56

+1

为我工作... – sehummel 2015-02-11 22:29:03

+1

这适用于5.0.1以来的所有HTTP错误代码响应。所以你可以有500,401,403等的自定义视图。 – Joe 2015-02-18 10:37:08

2

Angular HTML5模式可能会导致一堆缺少的页面(用户书签几页,并尝试重新加载)。如果您无法覆盖服务器设置来处理该问题,那么您可能会考虑忽略缺少的页面行为。

您可以修改\ App \ Exceptions \ Handler渲染方法以使用200推送内容,而不是使用404代码推送404模板。

/** 
* Render an exception into an HTTP response. 
* 
* @param \Illuminate\Http\Request $request 
* @param \Exception $e 
* @return \Illuminate\Http\Response 
*/ 
public function render($request, Exception $e) 
{ 
    if ($this->isHttpException($e) && $e->getStatusCode() == 404) 
    { 
     return response()->view('angular', []); 
    } 
    return parent::render($request, $e); 
} 
+0

也一样,但忘了'$ this-> isHttpException($ e)'。谢谢 – midan888 2015-03-04 08:16:47

-2

您可以处理它的一种方法,但它不是最好的方法是切换到重定向。

<?php namespace App\Exceptions; 

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

class Handler extends ExceptionHandler { 

     /** 
     * A list of the exception types that should not be reported. 
     * 
     * @var array 
     */ 
     protected $dontReport = [ 
       'Symfony\Component\HttpKernel\Exception\HttpException' 
     ]; 

     /** 
     * Report or log an exception. 
     * 
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc. 
     * 
     * @param \Exception $e 
     * @return void 
     */ 
     public function report(Exception $e) 
     { 
       return parent::report($e); 
     } 

     /** 
     * Render an exception into an HTTP response. 
     * 
     * @param \Illuminate\Http\Request $request 
     * @param \Exception $e 
     * @return \Illuminate\Http\Response 
     */ 
     public function render($request, Exception $e) 
     { 
     return redirect('/'); 
       //return parent::render($request, $e); 
     } 

} 
0

Laravel自带默认的错误页面,您可以轻松地添加自定义错误页这样的

1 - 创建视图的错误观点 - >错误的文件夹
2 - 必须匹配,如HTTP错误名称404或403 500

`Route::get('/page/not/found',function($closure){ 
    // second param is optional 
    abort(404, 'Page Not found'); 
});` 
0

通过添加以下代码

protected $dontReport = [ 
      'Symfony\Component\HttpKernel\Exception\HttpException' 
    ]; 

public function render($request, Exception $e) 
    { 
    return redirect('/'); 
      //return parent::render($request, $e); 
    } 

将正常工作对我来说

0

如果你想保持处理器在您的网页routes文件,您现有的路线后:

Route::any('{all}', function ($missingRoute) { 
    // $missingRoute 
})->where('all', '(.*)');