Most of the time web applications requires that error page should be styled differently than default error page provided by Laravel framework. This tutorial provides example how to create custom HTTP error page in Laravel 9 application.
Laravel framework allows making custom error pages for various HTTP status codes. For example, create the 404.blade.php
view template in the resources/views/errors
directory to customize error page for 404 HTTP status code.
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Error</title>
</head>
<body>
<p>404 Not Found</p>
</body>
</html>
Similarly, we can create error pages for other HTTP status codes such as 400.blade.php
, 500.blade.php
, etc.
Laravel provides default error page templates which can be published with Artisan command:
php artisan vendor:publish --tag=laravel-errors
Once the command has been finished, we can customize error pages.
A fallback error page for a given series of HTTP status codes can be created as well. This page will be displayed if an error page for specific HTTP status code is not created. We can create the 4xx.blade.php
and 5xx.blade.php
templates in the resources/views/errors
directory.
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Error</title>
</head>
<body>
<p>{{ $exception->getStatusCode() }} Something went wrong</p>
</body>
</html>
Leave a Comment
Cancel reply