In this tutorial, we will learn how to create a custom 404 error page in laravel application. You can handle any type of error by creating a specific error blade template in the resource folder.
You can easily create custom error pages for 404 and also for other errors like 403, 500, 419, 255, 405. While working on any web application project the errors exception you can get at any time and you know laravel can handle them very easily and show them beautifully.
Laravel provides us errors handler class that you can find by visiting this path app/Exceptions/Handler.php. It will check all errors exceptions thrown in the application. You can configure every exception in this file and can generate the appropriate response.
So, throughout this tutorial, we will handle the default errors by creating the custom 404 error page template in laravel application. Let’s start step by step guide to handling the errors by custom 404 page.
Create Laravel Project
Let’s start by creating a fresh new laravel application using the following laravel composer command. Just type this command in your terminal and hit the enter button and wait for finishing the installation.
Create Custom 404 Error Page
After installation, you will get the default laravel application folder structure and default frontend view page. So now we will create a custom 404 error page in this laravel application.
To handle the laravel 404 error, you just need to create a blade template view in the ‘resources/views/errors/‘ directory. But before, you need to create the ‘errors‘ directory inside the ‘views‘ directory.
The file name should be ‘404.blade.php‘, so your file path be like ‘/resources/views/errors/404.blade.php‘. Basically, laravel has a default design for errors but if you created custom error files inside the errors folder then it will take over from there.
Save the above code in the custom 404.blade.php file, you can change the design of this page as per your requirements.
Now we will test this custom 404 error page is working fine or not.
First, we need to start the laravel application, just go to the project folder and then run the following command to start serving the application.
When it started successfully, then visit the non-existed link by following the application URL. For example, we don’t have a ‘/test’ link in our application, so when we try to visit this link laravel will redirect to the custom 404 error page.
So when laravel does not find the above link then it will redirect you to the custom 404 error page that we just have created in the ‘/resources/views/errors‘ directory.
The same process you can follow for other errors like 403, 500, 419, 255, and 405. You can create the custom error page for these errors and should be inside the ‘errors‘ folder.
The filename must belong to the error like the following.
I hope you like it. If you have any queries please ask me in the comment section, I’ll help you with that.