How to Laravel create a custom error page

How to Laravel create a custom error page

Step 1: Install Laravel Project

Run the following command to create a Laravel 7 project:

composer create-project --prefer-dist laravel/laravel blog

Step 2: Database Connection Setup

In your .env file, update the database connection details:

DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=management_system DB_USERNAME=root DB_PASSWORD=123456

Step 3: Error Pages Setup

You have the HTML content for several error pages. Here is an example of how each error page might look like:

401 Unauthorized

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>401 | Unauthorized</title> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> <link rel="icon" href="{{URL::to('src/files/extra-pages/404/1/img/favicon.ico')}}" type="image/x-icon"> <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,700" rel="stylesheet"> <link rel="stylesheet" type="text/css" href="{{URL::to('files/extra-pages/404/1/css/style.css')}}"> </head> <body> <div class="image"></div> <a href="{{url('login')}}" class="logo-link" title="back home"> <img src="{{URL::to('files/extra-pages/404/1/img/logo.png')}}" class="logo" alt="Soeng Souy" width="250em;"> </a> <div class="content"> <div class="content-box"> <h1>Oops! Error 401 Unauthorized.</h1> <p>The page you were looking for doesn't exist. We think the page may have moved.</p> </div> </div> <footer> <ul> <li><a href="{{route('/')}}">Home</a></li> <li><a href="{{route('/')}}">Search</a></li> <li><a href="{{route('/')}}">Help</a></li> <li><a href="{{route('/')}}">Trust & Safety</a></li> <li><a href="{{route('/')}}">Sitemap</a></li> </ul> </footer> <script src="{{URL::to('files/extra-pages/404/2/js/jquery.min.js')}}"></script> <script src="{{URL::to('files/extra-pages/404/2/js/bootstrap.min.js')}}"></script> </body> </html>

403 Forbidden

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>403 | Forbidden</title> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> <link rel="icon" href="{{URL::to('src/files/extra-pages/404/1/img/favicon.ico')}}" type="image/x-icon"> <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,700" rel="stylesheet"> <link rel="stylesheet" type="text/css" href="{{URL::to('files/extra-pages/404/1/css/style.css')}}"> </head> <body> <div class="image"></div> <a href="{{url('login')}}" class="logo-link" title="back home"> <img src="{{URL::to('files/extra-pages/404/1/img/logo.png')}}" class="logo" alt="Soeng Souy" width="250em;"> </a> <div class="content"> <div class="content-box"> <h1>Oops! Error 403 Forbidden.</h1> <p>The page you were looking for doesn't exist. We think the page may have moved.</p> </div> </div> <footer> <ul> <li><a href="{{url('login')}}">Home</a></li> <li><a href="{{url('login')}}">Search</a></li> <li><a href="{{url('login')}}">Help</a></li> <li><a href="{{url('login')}}">Trust & Safety</a></li> <li><a href="{{url('login')}}">Sitemap</a></li> </ul> </footer> <script src="{{URL::to('files/extra-pages/404/2/js/jquery.min.js')}}"></script> <script src="{{URL::to('files/extra-pages/404/2/js/bootstrap.min.js')}}"></script> </body> </html>

404 Not Found

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>404 | Not Found</title> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> <link rel="icon" href="{{URL::to('src/files/extra-pages/404/1/img/favicon.ico')}}" type="image/x-icon"> <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,700" rel="stylesheet"> <link rel="stylesheet" type="text/css" href="{{URL::to('files/extra-pages/404/1/css/style.css')}}"> </head> <body> <div class="image"></div> <a href="{{url('login')}}" class="logo-link" title="back home"> <img src="{{URL::to('files/extra-pages/404/1/img/logo.png')}}" class="logo" alt="Soeng Souy" width="250em;"> </a> <div class="content"> <div class="content-box"> <h1>Oops! Error 404 not found.</h1> <p>The page you were looking for doesn't exist. We think the page may have moved.</p> </div> </div> <footer> <ul> <li><a href="{{url('login')}}">Home</a></li> <li><a href="{{url('login')}}">Search</a></li> <li><a href="{{url('login')}}">Help</a></li> <li><a href="{{url('login')}}">Trust & Safety</a></li> <li><a href="{{url('login')}}">Sitemap</a></li> </ul> </footer> <script src="{{URL::to('files/extra-pages/404/2/js/jquery.min.js')}}"></script> <script src="{{URL::to('files/extra-pages/404/2/js/bootstrap.min.js')}}"></script> </body> </html>

419 Page Expired

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>419 | Page Expired</title> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> <link rel="icon" href="{{URL::to('src/files/extra-pages/404/1/img/favicon.ico')}}" type="image/x-icon"> <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,700" rel="stylesheet"> <link rel="stylesheet" type="text/css" href="{{URL::to('files/extra-pages/404/1/css/style.css')}}"> </head> <body> <div class="image"></div> <a href="{{url('login')}}" class="logo-link" title="back home"> <img src="{{URL::to('files/extra-pages/404/1/img/logo.png')}}" class="logo" alt="Soeng Souy" width="250em;"> </a> <div class="content"> <div class="content-box"> <h1>Oops! Error 419 Page Expired.</h1> <p>The page you were looking for doesn't exist. We think the page may have moved.</p> </div> </div> <footer> <ul> <li><a href="{{url('login')}}">Home</a></li> <li><a href="{{url('login')}}">Search</a></li> <li><a href="{{url('login')}}">Help</a></li> <li><a href="{{url('login')}}">Trust & Safety</a></li> <li><a href="{{url('login')}}">Sitemap</a></li> </ul> </footer> <script src="{{URL::to('files/extra-pages/404/2/js/jquery.min.js')}}"></script> <script src="{{URL::to('files/extra-pages/404/2/js/bootstrap.min.js')}}"></script> </body> </html>

429 Too Many Requests

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>429 | Too Many Requests</title> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> <link rel="icon" href="{{URL::to('src/files/extra-pages/404/1/img/favicon.ico')}}" type="image/x-icon"> <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,700" rel="stylesheet"> <link rel="stylesheet" type="text/css" href="{{URL::to('files/extra-pages/404/1/css/style.css')}}"> </head> <body> <div class="image"></div> <a href="{{url('login')}}" class="logo-link" title="back home"> <img src="{{URL::to('files/extra-pages/404/1/img/logo.png')}}" class="logo" alt="Soeng Souy" width="250em;"> </a> <div class="content"> <div class="content-box"> <h1>Oops! Error 429 Too Many Requests.</h1> <p>The page you were looking for doesn't exist. We think the page may have moved.</p> </div> </div> <footer> <ul> <li><a href="{{url('login')}}">Home</a></li> <li><a href="{{url('login')}}">Search</a></li> <li><a href="{{url('login')}}">Help</a></li> <li><a href="{{url('login')}}">Trust & Safety</a></li> <li><a href="{{url('login')}}">Sitemap</a></li> </ul> </footer> <script src="{{URL::to('files/extra-pages/404/2/js/jquery.min.js')}}"></script> <script src="{{URL::to('files/extra-pages/404/2/js/bootstrap.min.js')}}"></script> </body> </html>

500 Internal Server Error

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>500 | Internal Server Error</title> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> <link rel="icon" href="{{URL::to('src/files/extra-pages/404/1/img/favicon.ico')}}" type="image/x-icon"> <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,700" rel="stylesheet"> <link rel="stylesheet" type="text/css" href="{{URL::to('files/extra-pages/404/1/css/style.css')}}"> </head> <body> <div class="image"></div> <a href="{{url('login')}}" class="logo-link" title="back home"> <img src="{{URL::to('files/extra-pages/404/1/img/logo.png')}}" class="logo" alt="Soeng Souy" width="250em;"> </a> <div class="content"> <div class="content-box"> <h1>Oops! Error 500 Internal Server Error.</h1> <p>The page you were looking for doesn't exist. We think the page may have moved.</p> </div> </div> <footer> <ul> <li><a href="{{url('login')}}">Home</a></li> <li><a href="{{url('login')}}">Search</a></li> <li><a href="{{url('login')}}">Help</a></li> <li><a href="{{url('login')}}">Trust & Safety</a></li> <li><a href="{{url('login')}}">Sitemap</a></li> </ul> </footer> <script src="{{URL::to('files/extra-pages/404/2/js/jquery.min.js')}}"></script> <script src="{{URL::to('files/extra-pages/404/2/js/bootstrap.min.js')}}"></script> </body> </html>

503 Service Unavailable

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>503 | Service Unavailable</title> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> <link rel="icon" href="{{URL::to('src/files/extra-pages/404/1/img/favicon.ico')}}" type="image/x-icon"> <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,700" rel="stylesheet"> <link rel="stylesheet" type="text/css" href="{{URL::to('files/extra-pages/404/1/css/style.css')}}"> </head> <body> <div class="image"></div> <a href="{{url('login')}}" class="logo-link" title="back home"> <img src="{{URL::to('files/extra-pages/404/1/img/logo.png')}}" class="logo" alt="Soeng Souy" width="250em;"> </a> <div class="content"> <div class="content-box"> <h1>Oops! Error 503 Service Unavailable.</h1> <p>The page you were looking for doesn't exist. We think the page may have moved.</p> </div> </div> <footer> <ul> <li><a href="{{url('login')}}">Home</a></li> <li><a href="{{url('login')}}">Search</a></li> <li><a href="{{url('login')}}">Help</a></li> <li><a href="{{url('login')}}">Trust & Safety</a></li> <li><a href="{{url('login')}}">Sitemap</a></li> </ul> </footer> <script src="{{URL::to('files/extra-pages/404/2/js/jquery.min.js')}}"></script> <script src="{{URL::to('files/extra-pages/404/2/js/bootstrap.min.js')}}"></script> </body> </html>

Conclusion

You have successfully set up the error pages for various HTTP statuses. If you're facing any issues with them or need further assistance, feel free to ask!


Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close