我在我的雲服務器上部署我的laravel應用程序。問題是,我不希望用戶訪問由應用程序導致的任何錯誤頁面(跟蹤堆棧),如何通過提供自定義錯誤頁面來阻止這種情況的發生?Laravel 4 - 生產站點自定義錯誤頁面
3
A
回答
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的,希望它可以幫助
相關問題
- 1. Laravel 4顯示自定義錯誤頁只在生產
- 2. Laravel:更改生產錯誤頁面
- 3. 自定義錯誤頁面asp.net mvc 4
- 4. 自定義錯誤頁面在生產中不工作
- 5. Laravel 4 - 自定義錯誤報告
- 6. Laravel 4工會產生錯誤的SQL
- 7. 自定義實體產生錯誤
- 8. IIS自定義錯誤未顯示自定義錯誤頁面
- 9. 自定義404錯誤頁面生成一個500錯誤
- 10. 如何使自定義錯誤頁面出現在生產服務器上
- 11. 刪除生產站點中的laravel 4.1錯誤消息
- 12. SIteCore - 多站點 - 顯示來自錯誤站點的頁面
- 13. 基於Laravel的軟件包自定義錯誤頁面
- 14. 自定義錯誤500頁面不顯示使用Event :: override - Laravel
- 15. Laravel 5.4顯示自定義的錯誤在登錄頁面
- 16. 如何在laravel 5中獲得自定義錯誤頁面?
- 17. 如果授權失敗,Laravel自定義錯誤頁面
- 18. 添加自定義基類爲我的asp.net頁面產生錯誤
- 19. 在生產現場顯示asp.net mvc 2.0中的自定義錯誤頁面
- 20. Magento產品頁面上的自定義錯誤消息
- 21. ASP.net當webservice發生錯誤時,自定義錯誤頁面重定向失敗?
- 22. Laravel 4生成URL錯誤
- 23. 在ASP.Net網站上實現自定義錯誤頁面
- 24. 將自定義錯誤頁面添加到網站
- 25. 在生產站點上創建頁面的空白頁面
- 26. Laravel 4錯誤:未定義的變量
- 27. symfony2自定義錯誤頁面
- 28. nGinx自定義錯誤頁面
- 29. SparkJava自定義錯誤頁面
- 30. Apache2 GeoIP/mod_geoip2自定義錯誤頁面