Laravel 5.0 - Custom Error Pages

Posted on February 01, 2015

Warning: This post is over a year old. I don't always update old posts with new information, so some of this information may be out of date.

When you wanted to customize your error pages—for example, showing a particular cat GIF when your users hit a 404—you’d Google it and follow these instructions by Dries Vints.

In Laravel 5 things have changed a bit. TL;DR take me to the solution already

Trace it through the code

Now, all custom error and exception handling has moved to app/Exceptions/Handler.php. You’ll remember that that’s where we went to bring Whoops back.

You’ll notice, however, that it does this by default:

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $e
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $e)
    {
        if ($this->isHttpException($e))
        {
            return $this->renderHttpException($e);
        }
        else
        {
            return parent::render($request, $e);
        }
    }

For all HTTP Exceptions (like 404s and 503s), it uses the renderHttpException() method, which isn’t defined in this file. So, we check its parent, \Illuminate\Foundation\Exceptions\Handler, where we can find the renderHttpException() method:

    /**
     * Render the given HttpException.
     *
     * @param  \Symfony\Component\HttpKernel\Exception\HttpException  $e
     * @return \Symfony\Component\HttpFoundation\Response
     */
    protected function renderHttpException(HttpException $e)
    {
        if (view()->exists('errors.'.$e->getStatusCode()))
        {
            return response()->view('errors.'.$e->getStatusCode(), [], $e->getStatusCode());
        }
        else
        {
            return (new SymfonyDisplayer(config('app.debug')))->createResponse($e);
        }
    }

So, if the view exists for "errors.{httpStatusCode}", it'll display it (and pass along a little bit of information).

How ya do it

So, if we have a view file accessible at “errors.{errorStatusCode}”, it’ll automatically display for that status code.

So that means customizing your 404 error page is as simple as adding a view at resources/views/errors/404.blade.php. Done!


Comments? I'm @stauffermatt on Twitter


Tags: laravel  •  laravel 5


(This is part of a series of posts on New Features in Laravel 5.0. )

  1. Sep 10, 2014 | laravel, 5.0, laravel 5
    In creating FormRequest, Taylor separated out both an interface (ValidatesWhenResolved) and a trait (ValidatesWhenResolvedTra...
  2. Sep 10, 2014 | laravel, 5.0, laravel 5
    Laravel 5.0 Laravel 5.0 is coming out in November, and there are a lot of features that have folks excited. The New Directory...
  3. Sep 12, 2014 | laravel, laravel 5, 5.0
    One of the big reasons Laravel 4.3 got moved to 5.0 is because the directory structure can seem like a big shift in thinking....
  4. Sep 20, 2014 | laravel, 5.0, laravel 5
    Performance optimization within our PHP code isn't always something at the forefront of our minds, but our backend code--espe...
  5. Sep 28, 2014 | laravel, laravel 5, 5.0
    In Laravel 5, you can now use the same Filesystem class which previously simplified interactions with the local filesystem, a...
  6. Sep 30, 2014 | laravel, 5.0, laravel 5
    Laravel 5.0 introduces the ability for the Container to resolve depencies injected into any methods that are resolved by the ...
  7. Oct 9, 2014 | laravel, 5.0, laravel 5
    Note: Route Annotations were eventually removed from core, and separated to a package maintained by the Laravel Community. T...
  8. Oct 10, 2014 | laravel, 5.0, laravel 5
    If you've been following along with my previous blog posts about Laravel 5.0, you may have noticed that route filters were fi...
  9. Oct 10, 2014 | laravel, 5.0, laravel 5
    Note: Event Annotations were eventually removed from core, and separated to a package maintained by the Laravel Community. T...
  10. Oct 16, 2014 | laravel, 5.0, laravel 5
    If you've followed my blog for a while, you've probably seen me struggle with Laravel's environment detection--especially reg...
  11. Nov 20, 2014 | laravel, 5.0, laravel 5
    Eric Barnes has a longer write up on this, so I'll just keep it short: Laravel 5.0 is introducing a pretty incredible cron-st...
  12. Jan 2, 2015 | laravel, 5.0, commands, laravel 5
    As you’ve probably read me mention on Twitter, I’ve paused on blogging new Laravel 5 features to try to give Taylor some ...
  13. Jan 16, 2015 | laravel, laravel 5
    It's very simple to get started in a new Laravel 5 app: composer create-project laravel/laravel my-project-name-here dev-deve...
  14. Jan 19, 2015 | laravel 5, laravel
    You might be missing the "prettier" Whoops error handler from Laravel 4. If so, here's how to bring it back. Updat...
  15. Jan 21, 2015 | laravel, events, 5.0, laravel 5
    If you haven't read it yet, go read the Laravel 5.0 - Commands & Handlers post. It'll give much-needed background for th...
  16. Jan 26, 2015 | laravel, laravel 5
    Sometimes it can seem like a lot of work to create an event, create its handler, and bind the two. Create a command, create i...
  17. Feb 1, 2015 | laravel, laravel 5
    When you wanted to customize your error pages—for example, showing a particular cat GIF when your users hit a 404—you’d...
  18. Feb 14, 2015 | laravel 5, laravel, eloquent
    I had completely forgotten to finish my Laravel 5.0 blog posts, but I saw a great quick introduction to attribute casting at ...

Subscribe

For quick links to fresh content, and more thoughts that don't make it to the blog