Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4 display custom error pages only in production

Tags:

laravel-4

Do I understand right: if I want to display custom pages for 403, 404 etc. errors I should check whether app.debug is set to false:

if (!Config::get('app.debug')) {
    App::error(function(Exception $exception, $code)
    {
        switch ($code) {
            case 403:
                return Response::view('errors.403', array(), 403);

            case 404:
                return Response::view('errors.404', array(), 404);

            case 500:
                return Response::view('errors.500', array(), 500);

            default:
                return Response::view('errors.default', array(), $code);
        }
        Log::error($exception);
    });
}

Because, if I set App::error handler without checking app.debug I'll always get these custom pages instead of detailed info

Am I right?

like image 481
Victor Avatar asked Nov 25 '13 20:11

Victor


People also ask

How do I disable error reporting in Laravel?

By default, error detail is enabled for your application. This means that when an error occurs you will be shown an error page with a detailed stack trace and error message. You may turn off error details by setting the debug option in your app/config/app. php file to false .

How do I enable errors in Laravel?

Through your config/app. php , set 'debug' => env('APP_DEBUG', false), to true . Or in a better way, check out your . env file and make sure to set the debug element to true.

How do I create a custom 404 page in Laravel?

Create Custom 404 Error Page You need to create blade views for error pages, move to this path resources/views/ inside here create errors folder and within the directory create 404. blade. php file. It will redirect you to the 404 page if you don't find the associated URL.


1 Answers

You could just move the if statement into the app::error function. So that the error is always logged, and the custom error page is only shown when app.debug is false.

App::error(function(Exception $exception, $code)
{
    Log::error($exception);

    if (!Config::get('app.debug')) {
        return Response::view('errors.index', $code);
    }
});
like image 152
jpblancoder Avatar answered Jan 02 '23 17:01

jpblancoder