2014-10-10 67 views
3

我在我的雲服務器上部署我的laravel應用程序。問題是,我不希望用戶訪問由應用程序導致的任何錯誤頁面(跟蹤堆棧),如何通過提供自定義錯誤頁面來阻止這種情況的發生?Laravel 4 - 生產站點自定義錯誤頁面

回答

10

首先,編輯您的主app.php配置文件app/config/app.php並將debug選項設置爲false。

/* 
|-------------------------------------------------------------------------- 
| Application Debug Mode 
|-------------------------------------------------------------------------- 
| 
| When your application is in debug mode, detailed error messages with 
| stack traces will be shown on every error that occurs within your 
| application. If disabled, a simple generic error page is shown. 
| 
*/ 

'debug' => false, 

接下來,在你的app/start/global.php文件,將有一個應用程序錯誤處理程序。你可以勾選這個來返回你自己的錯誤視圖(而不是「Whoops,出錯了」頁面)。確保您還返回500錯誤代碼,以便瀏覽器(和搜索引擎)知道發生了什麼。

/* 
|-------------------------------------------------------------------------- 
| Application Error Handler 
|-------------------------------------------------------------------------- 
| 
| Here you may handle any errors that occur in your application, including 
| logging them or displaying custom views for specific errors. You may 
| even register several error handlers to handle different types of 
| exceptions. If nothing is returned, the default error view is 
| shown, which includes a detailed stack trace during debug. 
| 
*/ 

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

    return Response::view('pages.error', [], 500); 
}); 

你也可以考慮添加一個默認頁面,以便找不到東西 - 一個不錯的404頁面。

App::missing(function(Exception $exception, $code) 
{ 
    return Response::view('pages.missing', [], 404); 
} 

最後,如果你使用findOrFail()或路徑模型,在您的應用程序結合的任何地方,你需要處理Illuminate\Database\Eloquent\ModelNotFoundException,不會調用missing()處理程序。

App::error(function(Illuminate\Database\Eloquent\ModelNotFoundException $exception, $code) 
{ 
    return Response::view('pages.missing', [], 404); 
} 
2

您可以在routes.php創建自定義錯誤處理程序,如:

// 404 error 
App::missing(function($exception) { 
    return Response::view('errors.show', array('code' => 'http_error_404'), 404); 
}); 

// Exception: ModelNotFoundException 
App::error(function(ModelNotFoundException $exception) { 
    return Response::view('errors.show', array('code' => 'model_not_found'), 404); 
}); 

// Exception: MethodNotAllowedHttpException 
App::error(function(MethodNotAllowedHttpException $exception) { 
    Log::warning('MethodNotAllowedHttpException', array('context' => $exception->getMessage())); 
    return Response::view('errors.show', array('code' => 'http_error_404'), 404); 
}); 

// Exception: QueryException 
App::error(function(QueryException $exception) 
{ 
    return Response::view('errors.show', array('code' => 'query_error')); 
} 
0

這在documentation非常深入的討論。首先,應在生產站點的配置文件中將debug設置爲false。

然後,你需要捕捉各種錯誤代碼,並用你自己的觀點回應。

例如,爲了趕上404,使用:

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

或者捕捉任何致命錯誤:

App::fatal(function($exception) 
{ 
    // 
}); 
0

我是一個新鮮的laravel 5.1網站,我收到此500錯誤發現我的PHP版本設置爲舊5.4,我將其設置爲php7並修復了我的問題,我沒有更改任何其他設置,所以我的問題是一個PHP的,希望它可以幫助

相關問題