Friday, August 19, 2016

Laravel : Customizing the default Error Page

Hi everyone

Today we will learn how to customize the default error page in laravel

We all know the famous "Whoops, looks like something went wrong." page, now what if we can easily customize this to match our overall template. All we need to do is modify the default app/Exceptions/Handler.php file and override the convertExceptionToResponse method:

<?php namespace App\Exceptions;

use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\Debug\ExceptionHandler as SymfonyDisplayer;

class Handler extends ExceptionHandler
{
    /**
     * Convert the given exception into a Response instance.
     *
     * @param \Exception $e
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    protected function convertExceptionToResponse(Exception $e)
    {
        $debug = config('app.debug', false);

        if ($debug) {
            return (new SymfonyDisplayer($debug))->createResponse($e);
        }

        return response()->view('errors.default', ['exception' => $e], 500);
    }
}

Now we can easily customize errors.default view located under resources/view/errors folder to our application need.

We still include the original error page when using APP_DEBUG=true for development, but we can remove those line if we don't want it.

1 comment:

  1. Quickbooks Support - More information visit our website https://www.quickbookconsulting.net/

    ReplyDelete