Skip to content

Instantly share code, notes, and snippets.

@loranger
Last active October 31, 2018 19:35
Show Gist options
  • Save loranger/c241f2ff515871e7edd4 to your computer and use it in GitHub Desktop.
Save loranger/c241f2ff515871e7edd4 to your computer and use it in GitHub Desktop.
Custom error page with default fallback for Laravel 5.1

Add the following method to you existing app/Exception/Handler.php in order to use resources/views/errors/$STATUS.blade.php (or fallback to resources/views/errors/default.blade.php)

   /**
    * Render the given HttpException.
    *
    * @param  \Symfony\Component\HttpKernel\Exception\HttpException  $e
    * @return \Symfony\Component\HttpFoundation\Response
    */
    protected function renderHttpException(HttpException $e)
    {
        if (config('app.debug', false)) {
            return $this->convertExceptionToResponse($e);
        }
        
        $status = $e->getStatusCode();
        $view = (view()->exists("errors.{$status}")) ? "errors.{$status}" : "errors.default";
        return response()->view($view, ['exception' => $e], $status);
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment