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
153
Related Errors
PHP
Call to a Member Function on Null
The code calls a method on a variable that is null instead of an object.
419
Laravel
TokenMismatchException (CSRF)
The CSRF token validation failed during a POST, PUT, or DELETE request.
403
Laravel
AuthorizationException (403)
The authenticated user lacks permission to perform the requested action.
Database
Data Truncation
The data being inserted or updated is too long for the column's defined size.