Fix "Target class [X] does not exist" and "Class not found" in Laravel
Target class [App\Http\Controllers\UserController] does not exist.
- Illuminate\Contracts\Container\BindingResolutionException: Target class [UserController] does not exist.
- Error: Class "App\Models\Post" not found
- PHP Fatal error: Uncaught Error: Class 'App\Helpers\Format' not found
- Target class [view] does not exist.
- Class "Laravel\Telescope\TelescopeApplicationServiceProvider" not found
Laravel or PHP cannot load the requested class. Check that the namespace exactly matches the file path (including letter case), use the [UserController::class, 'index'] syntax in routes, then run composer dump-autoload and php artisan optimize:clear.
What this error means
Laravel builds controllers, services and providers through its container. When it gets a class name, it asks the Composer autoloader to find the matching file. With PSR-4, App\Http\Controllers\UserController must live in app/Http/Controllers/UserController.php. If the file is not there, if the declared namespace differs, or if the class is missing from the autoload map, the container throws "Target class [X] does not exist." and PHP throws "Class "X" not found". The name in brackets is what Laravel looked for: if it has no namespace, the problem is how the class is referenced.
Common causes
- String route without a namespace: since Laravel 8,
'UserController@index'no longer gets an automaticApp\Http\Controllersprefix. - Namespace or case differs from the path:
namespace App\Http\Controllers\Admin;in a file stored elsewhere, oruserController.phpinstead ofUserController.php. It works on macOS and Windows, not on Linux. - Missing
use: the class is used in a file without being imported, so PHP looks for it in the current namespace. - Stale autoload or caches: a new class outside PSR-4, an optimized
classmap, orbootstrap/cachereferencing a removed provider. - Package not installed in production: a dev provider (Telescope, Debugbar) is registered while
composer install --no-devdid not install it.
Step-by-step fix
-
Read the name in brackets
If the namespace is missing (
[UserController]), the reference is incomplete. If it is complete ([App\Http\Controllers\UserController]), the file or the autoload is the problem. -
Reference controllers by class
Import the controller and use the array syntax: the IDE and PHP then check the full name.
use App\Http\Controllers\UserController; use Illuminate\Support\Facades\Route; Route::get('/users', [UserController::class, 'index'])->name('users.index'); -
Align namespace, file name and case
The namespace must follow the path from
app/and the file name must match the class name exactly, including capitals. To fix case with Git, usegit mvso the change is tracked.<?php // app/Http/Controllers/Admin/UserController.php namespace App\Http\Controllers\Admin; use App\Http\Controllers\Controller; class UserController extends Controller { public function index() { return view('admin.users.index'); } } -
Regenerate the Composer autoload
Check the
autoloadsection ofcomposer.jsonif the class lives in a custom folder, then regenerate the autoload.{ "autoload": { "psr-4": { "App\\": "app/", "Database\\Factories\\": "database/factories/", "Database\\Seeders\\": "database/seeders/" } } } -
Clear the Laravel caches
Run
composer dump-autoloadthenphp artisan optimize:clear. Ifartisanitself crashes on boot (for example "Target class [view] does not exist"), delete the PHP files inbootstrap/cacheby hand, then runcomposer dump-autoloadagain.composer dump-autoload php artisan optimize:clear # if artisan does not boot (macOS / Linux) rm -f bootstrap/cache/*.php && composer dump-autoload # if artisan does not boot (Windows PowerShell) Remove-Item bootstrap\cache\*.php; composer dump-autoload -
Register dev providers only locally
If the missing class comes from a
require-devpackage, do not register it inbootstrap/providers.php: load it conditionally, otherwise production crashes aftercomposer install --no-dev.// app/Providers/AppServiceProvider.php public function register(): void { if ($this->app->environment('local') && class_exists(\Laravel\Telescope\TelescopeServiceProvider::class)) { $this->app->register(\Laravel\Telescope\TelescopeServiceProvider::class); $this->app->register(TelescopeServiceProvider::class); } }
Did this fix work for you?
Confirm it in one click: it will rise for the next developers stuck on the same error.
Keep it from coming back
- Generate classes with
php artisan make:controller,make:model, etc.: namespace and path always match. - Run CI on Linux with
composer dump-autoload --strict-psrto catch files whose namespace does not match their path. - In deployment scripts, run
php artisan optimize:clearthenphp artisan optimizeafter everycomposer install.
Frequently asked questions
Why "Target class does not exist" after upgrading to Laravel 8 or later?
The RouteServiceProvider $namespace property is no longer used, so 'UserController@index' routes cannot find the class anymore. Switch to [UserController::class, 'index'].
Why is the class found locally but not on the server?
Almost always a case issue: macOS and Windows ignore the difference between User.php and user.php, Linux does not. Also check that composer install and optimize:clear ran during deployment.
What does composer dump-autoload do?
It regenerates the files in vendor/composer that tell PHP where each class lives. It is needed after changing the autoload section or adding a class outside PSR-4.
Useful tools for this error
More PHP & Laravel errors
- Fix "419 Page Expired" in Laravel (CSRF token mismatch)
- Fix "Allowed memory size of 134217728 bytes exhausted" in PHP
- Fix "SQLSTATE[HY000] [2002] Connection refused" in Laravel
- Fix "No application encryption key has been specified" in Laravel
- Fix "Your requirements could not be resolved to an installable set of packages" (Composer)
- Fix "laravel.log could not be opened in append mode: Permission denied"
- Fix "Vite manifest not found at: public/build/manifest.json" in Laravel