5

I have a problem with the Route::resource() method in Laravel 8.x. The error it returns is:

Target class [Admin\App\Http\Controllers\Admin\ProfileController] does not exist.

enter image description here

Here is my code in routes/web.php:

Route::prefix('admin')->namespace('Admin')->group(static function() {

    Route::middleware('auth')->group(static function () {
        //...
        Route::resource('profile', ProfileController::class);
    });
});

I could not find where the problem is.

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
Alisher Nasrullayev
  • 565
  • 1
  • 6
  • 22
  • 1
    Exactly what it says, there's no such file/class. Check if you named it properly and if you namespaced it correctly – Vladan Sep 11 '20 at 11:44
  • 1
    I think you just need to change `ProfileController::class` to `'App\Http\Controllers\Admin\ProfileController'` – STA Sep 11 '20 at 15:05

9 Answers9

15

Finally, I found out the answer in laravel 8.x upgarade guide. I have texted the controller name with full namespace, instead of importing it.

Route::prefix('admin')->namespace('Admin')->group(static function() {

    Route::middleware('auth')->group(static function () {
        //...
        Route::resource('profile', '\App\Http\Controllers\Admin\ProfileController');
    });
});
Alisher Nasrullayev
  • 565
  • 1
  • 6
  • 22
7

Run following step for clear route cache

php artisan route:clear
4
Route::resource('invoice','\App\Http\Controllers\InvoiceController');
Mobarak Hossen
  • 767
  • 5
  • 7
3

Make sure you followed the upgrade guide. There have been quite a few things that changed from v7 to v8.

To App/Providers/RouteServiceProvider.php add $namespace

class RouteServiceProvider extends ServiceProvider
{
    /**
     * This namespace is applied to your controller routes.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $namespace = 'App\Http\Controllers';
}

You also find more answers here: https://stackoverflow.com/a/63808132/799176

itskawsar
  • 1,232
  • 1
  • 19
  • 30
2

I also faced same issue with Laravel 7 latest version. See how I solved it:

First include this directory on the page
enter image description here use \App\Http\Controllers\Admin\ProfileController

Then call the full version of the route including the className like this

Route::resource('profile', '\App\Http\Controllers\Admin\ProfileController');

This will automatically create different routes for all methods defined in the ProfileController class. See an example in the image attached using TodoController.

Procrastinator
  • 2,526
  • 30
  • 27
  • 36
  • Note that when this could works, the solution given in upgrade 8.x guide in routing section https://laravel.com/docs/8.x/upgrade#routing is set to null $namespace property in RouteServiceProvider like is suggested bellow https://stackoverflow.com/questions/63845754/route-resource-not-working-in-laravel-8-x#answer-65189944 – gvd Sep 12 '21 at 19:23
2

Running php artisan route:list , I was having the same problem.

Target class [App\Http\Controllers\App\Http\Controllers\CourseController] does not exist.

In my case , what worked was:

Changing the resource from : Route::resource('courses', CourseController::class); to Route::resource('courses', 'CourseController');

I didn't changed any namespace and I'm using laravel 7.2.

kröte
  • 96
  • 6
1

So the reason this is an issue is because Laravel 8 removed the default namespace on the RouteServiceProvider.

If you want to use the ProfileController::class functionality you need to reset the protected $namespace to be null in the RouteProvider to match the base code.

Otherwise it will prepend the $namespace to whichever class you pass.

so change

protected $namespace = null

Then remove the ->namespace('Admin') from your routes.php file

Brett
  • 1,951
  • 2
  • 28
  • 35
1

In Laravel 8, First, you have to import the controller like,

use App\Http\Controllers\ProfileController;

And then use the resource,

Route::resource('profile', 'App\Http\Controllers\ProfileController');

if you used it in class,

Route::resource('profile',[ProfileController::class, 'classname']);
Abbas Arif
  • 372
  • 4
  • 16
-1

Please use below code, it seem that you are using class without importing so first you need to import profilecontroller in web.php file.

use App\Http\Controllers\Admin\ProfileController;      ## insert path profilecontroller 
Route::prefix('admin')->namespace('Admin')->group(static function() {

    Route::middleware('auth')->group(static function () {
        //...
        Route::resource('profile', ProfileController::class);
    });
});