Laravel 4 - 生产网站自定义错误页面

时间:2014-10-10 04:16:18

标签: php laravel laravel-4

我正在我的云服务器上部署我的laravel应用程序。问题是,我不希望用户访问由应用程序引起的任何错误页面(具有跟踪堆栈),如何通过提供自定义错误页面来阻止这种情况发生?

4 个答案:

答案 0 :(得分: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文件中,您将拥有一个应用程序错误处理程序。你可以挂钩到这个来返回你自己的错误视图(而不是“哎呀,出错了。”页面)。确保您还返回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);
}

答案 1 :(得分: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'));
}

答案 2 :(得分:0)

documentation对此进行了详尽的讨论。首先,在生产站点的配置文件中,debug应设置为false。

然后,您需要捕获各种错误代码,并回复您自己的观点。

例如,要捕获404,请使用:

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

或者发现任何致命错误:

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

答案 3 :(得分:0)

我在一个新的laravel 5.1网站上得到这个500错误,我发现我的PHP版本设置在旧的5.4我把它设置为php7并修复我的问题,我没有改变任何其他设置所以我的问题是一个PHP一,希望它有所帮助