NotFoundHttpException
The requested URL does not match any defined route.
Explanation
NotFoundHttpException is thrown when no route matches the incoming request URL. Unlike ModelNotFoundException which means a route matched but the record wasn't found, this means no route was defined for the URL at all. This commonly happens after changing route definitions, when visiting URLs with incorrect casing, or when route groups have path prefixes you forgot about. Laravel returns a 404 status code by default for this exception.
Common Causes
- Route not defined for the URL
- Incorrect route group prefix
- Stale route cache
- URL casing mismatch
- Routes file not loaded in RouteServiceProvider
Solution
Run php artisan route:list to see all defined routes and verify your URL matches one of them. Check for typos in route definitions and URLs. Ensure route group prefixes are correct. Verify that your routes file is being loaded (check RouteServiceProvider). If the route existed before, check if a cache is stale by running php artisan route:clear. For SPA applications, ensure your catch-all route is defined for client-side routing.
Code Example
// This route doesn't exist, causing NotFoundHttpException
// GET /api/users/profile
// Define the route properly
Route::get('/api/users/profile', [UserController::class, 'profile']);
// For SPAs, add a catch-all route
Route::get('/{any}', function () {
return view('app');
})->where('any', '.*');
// For API versioning, ensure prefix matches
Route::prefix('api/v1')->group(function () {
Route::get('/users', [UserController::class, 'index']);
});
// Must access as /api/v1/users, not /api/users
Error Information
Language
php
Framework
laravel
Difficulty
Beginner
Views
27
Related Errors
Laravel
SerializationException
Failed to serialize or unserialize data for caching, queues, or sessions.
DevOps
Laravel Schedule Task Failure
A scheduled task failed to execute or completed with errors.
PHP
Deprecated Function Usage
The code uses a function that has been deprecated and may be removed in future PHP versions.
PHP
Cannot Use String Offset as Array
The code tries to access a string using array syntax, treating it as an array.