The 5-Minute Fix for Laravel's "Target Class Does Not Exist" Error
You add a route, refresh the page, and Laravel answers with a 500 error: Illuminate\Contracts\Container\BindingResolutionException Target class [UserController] does not exist. Your controller file is right there in ap
You add a route, refresh the page, and Laravel answers with a 500 error:
Illuminate\Contracts\Container\BindingResolutionException
Target class [UserController] does not exist.
Your controller file is right there in app/Http/Controllers. You can see it. Laravel says it does not exist. Before you start doubting your installation, read the name inside the square brackets — it is the whole diagnosis, and it takes about ten seconds.
The brackets tell you the cause
Laravel tried to load exactly the class written between the brackets, and that string is almost never the class you wrote. Compare it with your real controller:
- [UserController] — no namespace at all. Laravel looked for a global class, which means your route uses the old string syntax or is missing its import.
- [App\Http\Controllers\UserController] — the full name is right, but the file is missing, misplaced, or invisible to the autoloader.
- [App\Http\Controllers\Api\UserController] — Laravel expected the controller in an Api subfolder, and something about that path disagrees.
- [auth.admin] — not a controller at all. A middleware alias that was never registered.
Fix 1: stop using string routes
This is the cause in most cases, and it comes from a change Laravel made in version 8. Before Laravel 8, the framework automatically prefixed string routes with App\Http\Controllers, so this worked:
Route::get('/users', 'UserController@index');
Since Laravel 8, that prefix is gone. The same line now makes Laravel look for a bare global UserController, which does not exist. Switch to the array syntax with an explicit import:
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
Route::get('/users', [UserController::class, 'index']);
This works in Laravel 8 through 13, and because UserController::class is a real class reference, your IDE can autocomplete it and warn you about typos before the code runs.
Fix 2: check the import, then the namespace
If the route already uses the array syntax, look at the top of the route file. Without the use App\Http\Controllers\UserController; line, PHP resolves the name in the global namespace and you get the same error.
If the bracketed name is a full namespace, check that the file's namespace line matches its folder. Composer's PSR-4 autoloader is mechanical: App\Http\Controllers\Api\UserController must live in app/Http/Controllers/Api/UserController.php and declare namespace App\Http\Controllers\Api;. Moving a controller into a subfolder without updating its namespace line is the classic way to break this. Run composer dump-autoload afterwards — Composer will warn you if a class still violates the PSR-4 rule.
One more trap: the filename is case-sensitive. usercontroller.php loads fine on Windows and macOS, then fails on a Linux server. If the error only appears after deployment, check the capitalisation.
Fix 3: clear the stale caches
When everything is spelled correctly and the error still appears, Composer or Laravel is working from old information:
composer dump-autoload
php artisan optimize:clear
And if you deploy with php artisan route:cache, rebuild it after every route change — Laravel serves requests from the compiled file in bootstrap/cache/routes-v7.php, not from your route files:
php artisan route:clear
php artisan route:cache
Then confirm with php artisan route:list --name=users. If the route points at the right class, the route layer is fixed.
The habit that prevents it
Create controllers with Artisan — php artisan make:controller Api/UserController — instead of by hand, because Artisan writes the correct namespace, filename and folder in one step. And run php artisan route:list after adding or moving routes, before opening the page in the browser. Two seconds, and a broken route never becomes a 500 error for your users.
I wrote a longer guide with all five fixes, including the middleware-alias case and a step-by-step diagnostic checklist, here: https://rajadevsolutions.online/errors/how-to-fix-target-class-does-not-exist-error-in-laravel
The error looks alarming, but it is one of the friendliest Laravel has: it tells you the exact class it wanted and could not find. Read the brackets, and the fix is usually one import, one namespace line, or one command away.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.