2013-11-25 40 views
7

難道我的理解對不對:如果我想顯示自定義頁403,404等錯誤,我應該檢查是否app.debug設置爲false:Laravel 4顯示自定義錯誤頁只在生產

if (!Config::get('app.debug')) { 
    App::error(function(Exception $exception, $code) 
    { 
     switch ($code) { 
      case 403: 
       return Response::view('errors.403', array(), 403); 

      case 404: 
       return Response::view('errors.404', array(), 404); 

      case 500: 
       return Response::view('errors.500', array(), 500); 

      default: 
       return Response::view('errors.default', array(), $code); 
     } 
     Log::error($exception); 
    }); 
} 

因爲,如果我設置App :: error handler而不檢查app.debug,我將永遠得到這些自定義頁面而不是詳細信息。

對嗎?

+0

我首先想到的是使用[Laravel的環境配置(http://four.laravel.com/docs/configuration#environment-configuration),但我可能是錯的。 –

+1

你想說使用App :: environment()而不是app.debug變量來檢查?我剛剛閱讀了關於環境的知識,這很棒,我可以在我的生產環境中自動覆蓋app.debug變量 – Victor

+0

您也可以使用'if(!App :: environment('local'){}' – JoshP

回答

10

您可以將if語句移動到app :: error函數中。所以這個錯誤總是被記錄下來,並且只有當app.debug爲false時纔會顯示自定義錯誤頁面。

App::error(function(Exception $exception, $code) 
{ 
    Log::error($exception); 

    if (!Config::get('app.debug')) { 
     return Response::view('errors.index', $code); 
    } 
}); 
+0

修復語法錯誤,在return語句中刪除額外的')'。 – Rashi