Laravel 5.0 - Custom Error Pages

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

  1. Laravel 5.0 - Form Requests
  2. Laravel 5.0 - ValidatesWhenResolved
  3. Laravel 5.0 - Directory structure and namespace
  4. Laravel 5.0 - Route Caching
  5. Laravel 5.0 - Cloud File Drivers
  6. Laravel 5.0 - Method Injection
  7. Laravel 5.0 - Route Annotations (removed)
  8. Laravel 5.0 - Event Annotations (removed)
  9. Laravel 5.0 - Middleware (Filter-style)
  10. Laravel 5.0 - Environment Detection & Environment Variables
  11. Laravel 5.0 - Event Scheduling
  12. Laravel 5.0 - Commands & Handlers
  13. Upgrading from Laravel 4 to Laravel 5
  14. Bringing Whoops Back to Laravel 5
  15. Laravel 5.0 - Events & Handlers
  16. Laravel 5.0 - Generating Missing Events
  17. Laravel 5.0 - Custom Error Pages
  18. Laravel 5.0 - Eloquent Attribute Casting

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

Matt Stauffer headshot

Hi, I'm Matt Stauffer.

I'm partner & technical director at Tighten Co.

You can find me on Twitter at @stauffermatt


Like what you're reading?

I wrote an entire 450+ page book for O'Reilly: Laravel: Up and Running.

You can order the eBook or print book today.