When you wanted to customize your —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 5 things have changed a bit. TL;DR take me to the solution already

Trace it through the code

Now, all 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  IlluminateHttpRequest  $request
     * @param  Exception  $e
     * @return IlluminateHttpResponse
     */
    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 3s), it uses the renderHttpException() method, which isn’t defined in this file. So, we check its parent, IlluminateFoundationExceptionsHandler, where we can find the renderHttpException() method:

    /**
     * Render the given HttpException.
     *
     * @param  SymfonyComponentHttpKernelExceptionHttpException  $e
     * @return SymfonyComponentHttpFoundationResponse
     */
    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!



Source link https://mattstauffer.com/blog/laravel-5.0-custom-error-pages

LEAVE A REPLY

Please enter your comment!
Please enter your name here