How to Solve Target class controller does not exist in Laravel Route Files
You add a new route, hit the URL, and Laravel throws this at you: Target class [App\Http\Controllers\UserController] does not exist. Or maybe: Target class [UserController] does not exist. This error is one of the most common ones when working with routes in Laravel. The good news is it usually comes down to a small mistake: namespaces, wrong path, missing import, or cached routes.
In this guide, we will walk through all the reasons this happens and how to fix each one.
1. Understand what the error means
When you define a route like this:
Route::get('/users', [UserController::class, 'index']);
Laravel tries to find the UserController class. If it cannot load it, you see:
Target class [UserController] does not exist.
So you need to make sure:
- The controller file exists.
- The class name matches the file name.
- The namespace matches what the route is using.
- Autoload and route cache are up to date.
2. Check the controller file and namespace
Open your controller file, for example:
app/Http/Controllers/UserController.php
You should see something like:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function index()
{
return view('users.index');
}
}
Pay attention to two things:
- File name:
UserController.php - Class name:
UserController - Namespace:
App\Http\Controllers
If the namespace does not match the folder path, Laravel will not be able to find the class.
For example, if the file is inside app/Http/Controllers/Admin, then the namespace should be:
namespace App\Http\Controllers\Admin;
And the route should use that full class.
3. Use the correct controller reference in routes
In routes/web.php or routes/api.php, you should reference the controller properly.
Option A: Use use import and ::class (recommended)
At the top of web.php:
use App\Http\Controllers\UserController;
Then define the route:
Route::get('/users', [UserController::class, 'index']);
This is the cleanest way and works in Laravel 8, 9, 10, and 11.
Option B: Use fully qualified class name directly
Without use, you can write:
Route::get('/users', [\App\Http\Controllers\UserController::class, 'index']);
This also works, but becomes messy with many routes.
4. Laravel 8+ change: no default namespace in RouteServiceProvider
In older versions (Laravel 7 and below), you could do:
Route::get('/users', 'UserController@index');
Because Laravel automatically prefixed App\Http\Controllers.
From Laravel 8 onwards, that default namespace was removed. So the old style string syntax without namespace will cause the “Target class does not exist” error.
Wrong in Laravel 8+:
Route::get('/users', 'UserController@index'); // likely to fail
Correct:
use App\Http\Controllers\UserController;
Route::get('/users', [UserController::class, 'index']);
If you migrated an old project to Laravel 11 and kept the old style strings, you will run into this error frequently.
5. Fixing controllers inside subfolders (Admin, Auth, etc.)
If your controller lives in a subfolder like app/Http/Controllers/Admin/UserController.php, then:
Controller:
<?php
namespace App\Http\Controllers\Admin;
class UserController extends Controller
{
// ...
}
Route file:
use App\Http\Controllers\Admin\UserController;
Route::get('/admin/users', [UserController::class, 'index']);
If the namespace is App\Http\Controllers\Admin, but the route still uses App\Http\Controllers\UserController, Laravel will not find the class and throws the error.
6. Run composer autoload if you just created the controller manually
If you created the controller file manually (copy/paste, not using artisan) and Laravel still says the class does not exist, try regenerating Composer’s autoload files:
composer dump-autoload
Then refresh your page.
Using artisan to create controllers is safer because it sets everything up correctly:
php artisan make:controller UserController
7. Clear route cache
If you are using route caching in production, Laravel may still be using an old cached version of your routes.
Clear and rebuild:
php artisan route:clear
php artisan route:cache
During local development you can simply keep caching off and only use:
php artisan route:clear
Then try the route again.
8. Double check spelling and case sensitivity
On Linux servers, file names and namespaces are case sensitive.
Make sure:
- File:
UserController.php(notuserController.php) - Class:
class UserController - Route import:
use App\Http\Controllers\UserController;
If any of these differ by even one letter, you will get the “Target class does not exist” error on the server, even if it worked on Windows locally.
9. Example: complete working setup
Here is a small working example to compare with your project.
Controller: app/Http/Controllers/PostController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function index()
{
return 'All posts list';
}
public function show($id)
{
return "Post ID: {$id}";
}
}
Routes: routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;
Route::get('/posts', [PostController::class, 'index'])->name('posts.index');
Route::get('/posts/{id}', [PostController::class, 'show'])->name('posts.show');
If this pattern works in your project, adjust your real controllers and routes to match this structure.
10. Quick checklist to fix “Target class controller does not exist”
- Confirm the controller file exists in
app/Http/Controllers/.... - Make sure the namespace in the controller matches its folder.
- Import the controller in
web.phporapi.phpusinguse App\Http\Controllers\YourController;. - Use
[YourController::class, 'method']syntax in routes. - Run
composer dump-autoloadif you added files manually. - Clear route cache with
php artisan route:clear. - Watch out for case sensitivity on Linux servers.
Once these pieces are aligned, the Target class controller does not exist error in your Laravel route files will disappear, and your routes will resolve to the correct controllers again.