Route resource not working in Laravel 8.x

Viewed 27425

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.

9 Answers

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

Run following step for clear route cache

php artisan route:clear
Route::resource('invoice','\App\Http\Controllers\InvoiceController');

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

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.

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.

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

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

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);
    });
});
Related