How to Create a Custom 404 Page in Laravel 10: Step-by-Step Guide
Introduction:
When developing a Laravel application, it is important to handle errors nicely and provide a smooth user experience. The most common error is the “404 — Page Not Found” error, which occurs when the user tries to access a path that does not exist. In Laravel 10, you can easily customize this error page to suit your brand and provide helpful navigation tips. In this guide, we will go through the steps to create a custom 404 page in Laravel 10.
Why Customize the 404 Page?
A custom 404 page can:
- Reflect your website’s branding.
- Provide a better user experience.
- Offer useful links or suggestions to keep users on your site.
- Make the error less frustrating by adding humor or helpful messages.
Step 1: Set Up the 404 View
Laravel, by default, stores error views in the resources/views/errors
directory. You’ll find a generic 404 page there, but to customize it, we’ll create our own.
- Navigate to the Errors Directory: Open your terminal and navigate to the
resources/views/errors
folder. If the folder does not exist, create it.
mkdir -p resources/views/errors
2. Create the 404 Blade View: Inside the errors
directory, create a new file named 404.blade.php
:
touch resources/views/errors/404.blade.php
3. Design the 404 Page: Now, let’s add some HTML to create a basic but elegant 404 page. You can customize the message and design based on your project needs.
<!-- resources/views/errors/404.blade.php -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Page Not Found</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap">
<style>
body {
font-family: 'Roboto', sans-serif;
background-color: #f8f9fa;
color: #333;
text-align: center;
padding: 50px;
}
h1 {
font-size: 72px;
margin-bottom: 40px;
color: #ff6b6b;
}
p {
font-size: 24px;
margin-bottom: 20px;
}
a {
color: #007bff;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
.container {
max-width: 600px;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="container">
<h1>404</h1>
<p>Oops! The page you're looking for can't be found.</p>
<p><a href="{{ url('/') }}">Go back to Home</a></p>
</div>
</body>
</html>
This creates a clean, user-friendly 404 page that you can modify with your brand’s colors, fonts, and logos.
Step 2: Handle the 404 Error in the RouteServiceProvider
Laravel itself shows error pages based on the HTTP status code, and if it is a 404 error, it will look for the 404.blade.php file in the resources/views/errors directory but if you want to change how Laravel handles these errors use to do so in which the app/Exceptions/Handler.php file can.
In most cases, you won’t need to modify this file, but if you need advanced logic (e.g., logging or redirecting), you can edit the render()
method like so:
// app/Exceptions/Handler.php
public function render($request, Throwable $exception)
{
if ($exception instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException) {
return response()->view('errors.404', [], 404);
}
return parent::render($request, $exception);
}
Step 3: Testing Your Custom 404 Page
Once you’ve set up your custom 404 page, it’s time to test it. You can do this by accessing a non-existent method in your Laravel application.
- Start Your Laravel Server: If you haven’t already, start your local Laravel development server:
php artisan serve
2. Visit a Non-Existent URL: Open your browser and go to any URL that doesn’t exist in your project, like http://localhost:8000/nonexistent-page
. You should see the custom 404 page you created.
Step 4: Additional Enhancements
Here are a few additional tweaks you can add to your custom 404 page:
- Add a Search Bar: Help users find what they’re looking for by adding a search bar to your 404 page.
- Link to Popular Pages: Include links to your most popular or recent content to guide users back into the site.
- Include a Contact Form: You can offer users the option to contact support if they landed on the 404 page by mistake.
<p>Can’t find what you’re looking for? <a href="{{ url('/contact') }}">Contact us</a> for help.</p>
Conclusion
Creating a custom 404 page in Laravel 10 is simple yet effective to improve user experience and retain visitors. By following the steps mentioned above, you can create a 404 page that not only showcases your brand but also helps users navigate your website more efficiently.
If you find this guide helpful, feel free to share it with others who want to improve their Laravel application!