Show a 404 page if route not found in Laravel 5.1

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Displaying a Custom 404 Page for Unfound Routes in Laravel 5.1 Body: In this post, we will explore how to display a custom 404 page when a route is not found in your Laravel application. We'll start by creating the required files and then understanding their roles in the process. Finally, we will look at some common issues and their solutions. 1. Create a 404 Template You need to create a dedicated template for your 404 page called `404.blade.php`. This file should be placed within the `resources/views/errors` directory of your project. You can use any HTML, CSS, or Laravel Blade syntax you prefer:
@extends('layouts.app')

@section('title', '404 - Not found')

@section('content')
    

Oops! It looks like this page doesn't exist.

We apologize for any inconvenience caused, but the requested resource could not be found. Kindly return to the previous page or try searching again.

@endsection
2. Configure the Handler.php File The `Handler.php` file is where you define the error handling for your application. In this case, we need to modify it to display a 404 page when a route is not found. Here's an example with comments:
public function render($request, Exception $e)
{
    if ($e instanceof TokenMismatchException) {
        // redirect to form an example of how i handle mine
        return redirect($request->fullUrl())->with(
            'csrf_error',
            "Opps! Seems you couldn't submit form for a longtime. Please try again"
        );
    }

 /*if ($e instanceof CustomException) {
        return response()->view('errors.404', [], 500);
    }*/

    if ($e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException)
        return response(view('error.404'), 404);

    return parent::render($request, $e);
}
As you can see, we check for the `NotFoundHttpException` exception, which is thrown when a route is not found in Laravel. We then return a response with a view of our 404 page. The commented-out code section shows an example of handling another exception (custom exception) that could be adapted to your needs. 3. Configuring the App.php File Make sure you have `'debug' => env('APP_DEBUG', true),` in your `app.php`. This setting allows you to see debug information on error pages if your application is in debug mode, which can help with troubleshooting. 4. Common Issues and Solutions Here are some common issues related to displaying a 404 page and their solutions: - If the 404 page is not displayed for incorrect URLs, ensure your `routes/web.php` file is properly configured and routes exist for those pages. You can also set redirect routes if you want users to be redirected to a different URL instead of displaying a blank page in this scenario. - If the 404 page displays with some errors or inconsistencies, ensure that your view files are valid Laravel Blade syntax and follow proper conventions (e.g., variable names). You can also try clearing your application cache and config caches to see if any cached data is causing issues. - If the 404 page does not display for specific URLs, verify the route definition in `routes/web.php` or other route configuration files matches the URL structure you are using. You might need to adjust the route name, domain, or other parameters accordingly. Conclusion: Ensuring your Laravel 5.1 application displays a custom 404 page is essential for providing an improved user experience and maintaining professional standards. By following these steps and troubleshooting common issues, you can have a functional error handling system in place. Happy coding!