user3583721
user3583721

Reputation: 327

Laravel 4 - production site customize error page

I'm deploying my laravel app on my cloud server. The thing is, I don't want users to reach any error page (with tracing stack) caused by the app, how do I stop this from happening by providing a customized error page?

Upvotes: 3

Views: 1812

Answers (4)

Vasko
Vasko

Reputation: 257

I was getting this 500 error on a fresh laravel 5.1 website and i found my php version was set on the old 5.4 and I set it to php7 and fixed my issue, I didnt change any other settings so my issue was a php one, hope it helps

Upvotes: 0

Mike Rockétt
Mike Rockétt

Reputation: 9007

This is discussed very thoroughly in the documentation. First of all, debug should be set to false in your configuration file for production sites.

Then, you need to catch the various error codes, and respond with your own views.

For example, to catch a 404, use this:

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

Or to catch any fatal errors:

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

Upvotes: 0

Thomas Jensen
Thomas Jensen

Reputation: 2198

You can create custom error handlers in your routes.php, like:

// 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'));
}

Upvotes: 2

Dwight
Dwight

Reputation: 12440

First off, edit your main app.php config file in app/config/app.php and turn the debug option to 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,

Next, in your app/start/global.php file you'll have an application error handler. You can hook into this to return your own error view (instead of the "Whoops, something went wrong." page). Make sure you also return the 500 error code so the browser (and search engines) know what is going on.

/*
|--------------------------------------------------------------------------
| 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);
});

You might also consider adding a default page for when something isn't found - a nice 404 page.

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

Finally, if you use findOrFail() or route model binding anywhere in your app you'll need to handle the Illuminate\Database\Eloquent\ModelNotFoundException which won't call the missing() handler.

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

Upvotes: 10

Related Questions