0

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:

capture

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;
5
  • I'm confused. What's actually working and what's not working? Which controller is found and which one is not? Commented Jan 23, 2021 at 6:26
  • @apokryfos ProductController is not found. I tried the both old and new ways to call this Controller, but still can not be found. Commented Jan 23, 2021 at 6:29
  • Please share the first lines of your ProductController.php file as well. Commented Jan 23, 2021 at 6:50
  • @AhmadKarimi I did, see UPDATE #1 plz Commented Jan 23, 2021 at 6:57
  • your namespace on that fie is wrong ... it is missing the Admin part of the namespace, as the answer below states Commented Jan 23, 2021 at 7:20

6 Answers 6

4

You just need go to app/Http/Providers/RouteServiceProvider.php

uncomment: protected $namespace = 'App\Http\Controllers';

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

3

Inside your ProductController, you need to have namespace like this

namespace App\Http\Controllers\Admin;

2 Comments

Thanks for the answer, I tried that but still receive: Target class [App\Http\Controllers\Admin\ProductController] does not exist.
The ProductController should have this namespace declared as it is in the Admin folder ... PSR4 autoloading
2

You need to declare your namespace:

use App\Http\Controllers\Admin\ProductController;

Then on your route:

Route::apiResource('products', ProductController::class);

Comments

0

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');

Comments

0

You have just to put the path instead the name of the Controller
You can change in your web.php:

Route::resource('products','ProductController');

to:

Route::resource('products','App\Http\Controllers\ProductController');

Comments

-1

you should remove line use namespace... controller in route page.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.