The error with code 404 may occur when something couldn’t be found. Very often, it happens when Laravel can’t find the requested route or the specified route is not registered. Also you can see it when a code throws HttpNotFoundException.
In this article we will explain how to debug 404 Not Found in Laravel. You will learn how to check registered routes, how to clear cache and how to install and use the laravel/telescope package to debug Laravel requests.
Table of Contents
Fixing 404 Not Found in Laravel
The laravel/telescope package provides the best and simplest way to debug requests in the Laravel framework. You can view all requests, their parameters, headers, and the handling process. The package can display what middleware and controller were used to handle each request.
Next I will show how to install and configure Telescope. I assume that you have installed the Laravel framework. If you get this error, may you want to create API. You can read in more detail about in the article How to Create REST API in Laravel.
1. Install Telescope
First, install the laravel/telescope package for your project using Composer:
composer require --dev laravel/telescope
Then, run publish migrations and configuration using the command below:
php artisan telescope:install
After running telescope:install command, create database tables using command:
php artisan migrate
Then, make sure that the telescope is enabled. Open config/telescope.php file and find enabled. You will see the following lines:
You can change the default value to false and enable Telescope in the .env file using the following variable:
TELESCOPE_ENABLED=true
2. Show Logged Requests
Note, that Telescope can only show the requests performed after package activation. Now, you can make any request to the Laravel project and get a 404 error. For example:
https://localhost:8080/test
Then open /telescope route. If your server has the address http://localhost:8080, the route will look like this http://localhost:8080/telescope. Here switch to the Requests tab:
You will see all logged requests. For example the latest request returned a 404 error code. Click on the eye icon to view details:
Here, you should take a look at Middleware and Controller Action fields. In this example they are empty. This means that the router can’t find the requested route. When the router finds a route, these fields will contain data about the controller and action handling this request. For example:
Here you can see that the request was handled by ExampleController and index action. This means that the router can find this route and if you get 404, the controller throws this error. It helps to understand where the problem is and fix it.
3. Check Routes
If the router can’t find your route but you think that you have registered it, you can view all registered routes using the command below:
php artisan route:list
If you have cached routes, you should clear the cache before you see new routes:
php artisan cache:clear
Also, consider Laravel framework has support for where() method for routes. For example, you can register the route /example/{id} where id can contain only digits from 0 to 9. When you use /example/5, it will work, but when you use /example/5a you will get a 404 error. If you want to use this method in your project, you should set the key name and regular expression for it. For example:
Route::get("/example/{id}", [
\App\Http\Controllers\ExampleController::class,
"index",
])
->name("example")
->where("id", "[0-9]+");
How to Trigger 404 Error Manually
The best and simplest way to trigger a 404 error manually is to throw NotFoundHttpException:
throw new NotFoundHttpException();
When you throw this exception, Laravel will render a standard 404 view. Also you can get a 404 error, when you use the findOrFail() method to get models.
Wrapping Up
In this article we have explained how to debug 404 errors in Laravel. It is pretty simple if you use a special tool. For example the laravel/telescope package. In additional, you can use this tool in many other cases, but be careful and enable it only for development.