'Laravel 8: Target class [ProductController] does not exist
I'm using Laravel-8 to develop my project, and I have a resource controller named ProductController which is placed at Admin directory inside Controllers, just like this image is showing:
Then at my route file, I coded this:
Route::resource('products', 'ProductController');
Route::resource('permissions', 'PermissionController');
But when I want to go to products route, I get this message:
Illuminate\Contracts\Container\BindingResolutionException Target class [App\Http\Controllers\Admin\ProductController] does not exist.
Now you may say in Laravel-8, I have to use Route::get('/', ProductController::class);, but as you can see above, I have also determined a permissions route to PermissionController by the old method and it is working completely fine!
The namespace of Admin is also specified at RouteServiceProvider:
Route::middleware(['web' , 'auth' , 'auth.admin'])
->namespace('App\Http\Controllers\Admin')
->prefix('admin')
->group(base_path('routes/web/admin.php'));
Note that I also tried Route::resource('products', ProductController::class); , but still get the same error.
I guess the issue is coming from another part!
So if you have any idea about this, please let me know, I would really appreciate any idea or suggestion from you guys...
Thanks in advance.
UPDATE #1:
ProductController goes like this:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Product;
Solution 1:[1]
Inside your ProductController, you need to have namespace like this
namespace App\Http\Controllers\Admin;
Solution 2:[2]
You just need go to app/Http/Providers/RouteServiceProvider.php
uncomment: protected $namespace = 'App\Http\Controllers';
Solution 3:[3]
You need to declare your namespace:
use App\Http\Controllers\Admin\ProductController;
Then on your route:
Route::apiResource('products', ProductController::class);
Solution 4:[4]
Not sure whether this answer would help anyone or not but, I think you haven't imported the ProductController into your api.php
There are two widely used ways to import in laravel:
1st way-
As @Talita suggested, import the file with the use syntax and then call out the class in Route::apiResource
use App\Http\Controllers\Admin\ProductController;
Route::apiResource('products', ProductController::class);
2nd way-
You can call the class's location within the Route::apiResource just as follows-
Route::apiResource('/products', '\App\Http\Controllers\Admin\ProductController');
Solution 5:[5]
you should remove line use namespace... controller in route page.
Solution 6:[6]
I think you forget to Commit the transaction, like this
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentActivity fragment = new DeterminaFrag();
FragmentManager fragmentManager = fragment.getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.nav_host_fragment_content_main, fragment);
fragmentManager.commit();
}
});
I hope it's can help your problem
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | KINGSLEY OKPARA |
| Solution 2 | |
| Solution 3 | Talita |
| Solution 4 | FireLordZuko |
| Solution 5 | Anh Ngá»c |
| Solution 6 | Fakhri Khairi |

