Custom error pages in Laravel 5

By Michael Williams, Published on 02/28/2016

Laravel 5 includes a 503 error template in /resources/views/errors by default.

To add a 404 or other error template. All you need to do is create the corresponding file within your errors directory. For example, 404.blade.php. This template will now be displayed whenever a user attempts to view a url that doesn't exist.

When working with a framework or code in general. It's a good idea to take a look beneath the hood and get a better understanding of what's going on. 

You'll want to look at the renderHttpException() method.

/**
* Render the given HttpException.
*
* @param \Symfony\Component\HttpKernel\Exception\HttpException $e
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function renderHttpException(HttpException $e)
{
$status = $e->getStatusCode();
if (view()->exists("errors.{$status}")) {
return response()->view("errors.{$status}", ['exception' => $e], $status, $e->getHeaders());
} else {
return $this->convertExceptionToResponse($e);
}
}


If a view exists within the errors directory for the status returned from the request (404, 403, etc..). That view will be returned along with some additional information.

Otherwise the exception itself will be return to the display.