kylemabaso's avatar

Illuminate\Contracts\Container\BindingResolutionException Target class [UserController] does not exist.

Hi Guys,

I'm new to Laravel. I started working on Laravel 7 a week ago, built an API and everything is working fine. I attempted to recreate the same API with Laravel 8 but my api routes don't seem to be working anymore. I get an error that says:

"Illuminate\Contracts\Container\BindingResolutionException Target class [UserController] does not exist."

I have defined api routes like so: Route::resource('users', 'UserController', ['except' => ['create', 'edit']]);

Kind regards,

Kyle

0 likes
17 replies
Sergiu17's avatar
Sergiu17
Best Answer
Level 60

Hi, I guess you have to specify full path to the class

Route::resource('users', 'App\Http\Controllers\UserController', ['except' => ['create', 'edit']]);

or

use App\Http\Controllers\UserController;

Route::resource('users', UserController::class, ['except' => ['create', 'edit']]);
14 likes
kylemabaso's avatar

Thank you so much for your prompt response, Kind Sir. That resolved my problem.

1 like
adityar15's avatar

@sergiu17 Thanks for the response. Seems like the Laravel 8.x update has a different way of using routes.

Previously it was

Route::get('test','TestController@test');

Now it has changed to

use App\Http\Controllers\TestController;
Route::get('test',[TestController::class, 'test']);

To do it the old style you need to add $namespace property in RouteServiceProvider.

Not sure why they made it bit complicated :)

13 likes
From The Rubble's avatar

I'm brand new at Laravel and I'm on Lesson 8 of getting started. Nothing was working until I found this. Thank you.

1 like
Sayay's avatar

@adityar15 this make my lesson to be easier, i spend two days finding this solution šŸ˜…

daugaard47's avatar

Came here with the same issue looking for the solution. Thank you for answering this. Is this documented anywhere? Couldn't find in the Docs. IDK... Seems like a small step backwards to me. ĀÆ\(惄)/ĀÆ

havish999's avatar

I am very new to laravel and learning it. Very interesting to learn. Started with 7 and now working on 8. I came here with the same error as stated. Followed the syntax as stated and it's resolved.

In the web.php file we have this statement

use Illuminate\Support\Facades\Route;

My doubt is do we require this?

codemode's avatar

Note, that the single quotation marks have been removed in the DemoController part below (Laravel 8). It's easy to miss them, i was confused too.

Till Laravel 7 :

Route::resource('demo','DemoController'); 

From Laravel 8:

use App\Http\Controllers\DemoController;

Route::resource('demo', DemoController::class);
2 likes
talzana55@gmail.com's avatar

Hello

I have read your answers which are all equally logical, but I am having a problem that I cannot find a solution to.

web.php

Route::get('/clients','app/Http/Controllers/ClientsController@List');

ClientsController

1 like
ricardov's avatar

This usually happens because you're using a different Controller Namespace in your class. In case you are not familiar with namespaces, I highly recommend reading more about it and get familiar with it. Composer 2 improve the PSR-4 autoloading standard and understand how to use it can avoid future issues.

Laravel includes a Namespace option on Route::groups to allows you to call the new namespace. With this, you avoid miss use of importation on your routes files and improve the readability of your code. This is how you can implement your need:

Imaging your new class is in app/Http/Controllers/Users/ folder and namespace, you can call this controller with the following route:

Route::group(["namespace"=>"Users"], function() {
	Route::resource('users', 'UserController', ["except" => ["edit"]]);
});
1 like
Mtanhaurwa's avatar

@ricardov For Laravel 7 that is the way to stop "Illuminate\Contracts\Container\BindingResolutionException Target class [......Controller] does not exist.". Commenting out protected $namespace = 'App\Http\Controllers' property in RouteServiceProvider will also do the trick. Lastly is to remove namespace($this->namespace) in boot method.

public function boot() { $this->configureRateLimiting();

    $this->routes(function () {
        Route::prefix('api')
            ->middleware('api')             
            ->group(base_path('routes/api.php'));

        Route::middleware('web')
            ->namespace($this->namespace)
            ->group(base_path('routes/web.php'));
    });
}
LindaElsa's avatar

Illuminate  \  Contracts  \  Container  \  BindingResolutionException PHP 8.3.3 11.0.8 Target class [App\Http\Controllers\Auth\Guard] does not exist.

please how should i fix it

Please or to participate in this conversation.