php dependency_injection ai_generated true

Illuminate\Contracts\Container\BindingResolutionException: Target class [X] does not exist

ID: php/laravel-container-binding-resolution

Also available as: JSON · Markdown
92%Fix Rate
90%Confidence
85Evidence
2020-09-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
83 active

Root Cause

The Laravel service container cannot resolve a binding because the target class doesn't exist, wasn't registered, or auto-discovery failed. Common after upgrading Laravel 8+ which removed default namespace prefix from RouteServiceProvider.

generic

Workarounds

  1. 95% success Use fully qualified class names in route definitions
    In routes/web.php, change:
    Route::get('/users', 'UserController@index');
    To:
    use App\Http\Controllers\UserController;
    Route::get('/users', [UserController::class, 'index']);

    Sources: https://laravel.com/docs/10.x/controllers#basic-controllers

  2. 93% success Register the binding explicitly in a ServiceProvider
    In AppServiceProvider::register():
    $this->app->bind(MyInterface::class, MyImplementation::class);
    // Or for singletons:
    $this->app->singleton(MyService::class, function ($app) {
        return new MyService($app->make(Dependency::class));
    });

    Sources: https://laravel.com/docs/10.x/container#binding

  3. 80% success Clear all cached configs, routes, and services
    php artisan config:clear && php artisan route:clear && php artisan cache:clear && composer dump-autoload

Dead Ends

Common approaches that don't work:

  1. Adding the full namespace prefix back to RouteServiceProvider::$namespace 60% fail

    Laravel 8+ intentionally removed the default namespace prefix to support PHP 8 named arguments and better IDE support. Re-adding it creates subtle routing issues with resource controllers and invokable controllers.

  2. Running composer dump-autoload repeatedly without checking the actual class path 70% fail

    dump-autoload regenerates the autoloader map, but if the class file doesn't exist or the namespace doesn't match the directory structure (PSR-4), re-dumping won't help.

Error Chain

Preceded by:
Frequently confused with: