2014-10-10 90 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的,希望它可以帮助