我在我的云服务器上部署我的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:更改生产错误页面
- 2. 自定义实体产生错误
- 3. 自定义404错误页面生成一个500错误
- 4. 如何使自定义错误页面出现在生产服务器上
- 5. 如果授权失败,Laravel自定义错误页面
- 6. 基于Laravel的软件包自定义错误页面
- 7. Laravel 5.4显示自定义的错误在登录页面
- 8. 添加自定义基类为我的asp.net页面产生错误
- 9. 在生产现场显示asp.net mvc 2.0中的自定义错误页面
- 10. 将自定义错误页面添加到网站
- 11. 在ASP.Net网站上实现自定义错误页面
- 12. ASP.net自定义错误页面
- 13. SharePoint 2007的自定义页面错误
- 14. Apache2.4.10自定义错误页面
- 15. 自定义错误页面InvalidArgumentException Symfony2
- 16. Index.php as自定义错误页面
- 17. 瓶颈JSON自定义错误页面
- 18. 自定义错误页面不显示
- 19. 更改自定义404错误页面
- 20. nGinx自定义错误页面
- 21. 自定义错误页面404
- 22. 没有自定义404错误页面
- 23. JBoss中的自定义错误页面
- 24. MVC5自定义错误页面
- 25. CakePHP Plugin的自定义错误页面
- 26. Kohana 2自定义404错误页面
- 27. sharepoint 2013自定义错误页面
- 28. Symfony PHP Template自定义错误页面
- 29. joomla中的自定义错误页面?
- 30. Appcache和自定义错误页面