Skip to content
FR
  1. LuzineDebug
  2. PHP & Laravel
  3. Target class [App\Http\Controllers\UserControlle…
PHP & Laravel Beginner ≈ 10 min to fix

Fix "Target class [X] does not exist" and "Class not found" in Laravel

Error message
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
In short

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.

Be the first to confirm this fix

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

  1. String route without a namespace: since Laravel 8, 'UserController@index' no longer gets an automatic App\Http\Controllers prefix.
  2. Namespace or case differs from the path: namespace App\Http\Controllers\Admin; in a file stored elsewhere, or userController.php instead of UserController.php. It works on macOS and Windows, not on Linux.
  3. Missing use: the class is used in a file without being imported, so PHP looks for it in the current namespace.
  4. Stale autoload or caches: a new class outside PSR-4, an optimized classmap, or bootstrap/cache referencing a removed provider.
  5. Package not installed in production: a dev provider (Telescope, Debugbar) is registered while composer install --no-dev did not install it.

Step-by-step fix

  1. 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.

  2. Reference controllers by class

    Import the controller and use the array syntax: the IDE and PHP then check the full name.

    php
    use App\Http\Controllers\UserController;
    use Illuminate\Support\Facades\Route;
    
    Route::get('/users', [UserController::class, 'index'])->name('users.index');
  3. 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, use git mv so the change is tracked.

    php
    <?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');
        }
    }
  4. Regenerate the Composer autoload

    Check the autoload section of composer.json if the class lives in a custom folder, then regenerate the autoload.

    json
    {
        "autoload": {
            "psr-4": {
                "App\\": "app/",
                "Database\\Factories\\": "database/factories/",
                "Database\\Seeders\\": "database/seeders/"
            }
        }
    }
  5. Clear the Laravel caches

    Run composer dump-autoload then php artisan optimize:clear. If artisan itself crashes on boot (for example "Target class [view] does not exist"), delete the PHP files in bootstrap/cache by hand, then run composer dump-autoload again.

    bash
    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
  6. Register dev providers only locally

    If the missing class comes from a require-dev package, do not register it in bootstrap/providers.php: load it conditionally, otherwise production crashes after composer install --no-dev.

    php
    // 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-psr to catch files whose namespace does not match their path.
  • In deployment scripts, run php artisan optimize:clear then php artisan optimize after every composer 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

All PHP & Laravel errors →