2014-10-29 42 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', '(.*)');