Route resource not working in Laravel 8.x

19,892

Solution 1

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

Solution 2

Run following step for clear route cache

php artisan route:clear

Solution 3

Route::resource('invoice','\App\Http\Controllers\InvoiceController');

Solution 4

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

Solution 5

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.

Share:
19,892
Alisher Nasrullayev
Author by

Alisher Nasrullayev

Updated on June 13, 2022

Comments

  • Alisher Nasrullayev
    Alisher Nasrullayev almost 2 years

    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.

  • Alisher Nasrullayev
    Alisher Nasrullayev over 3 years
    I have imported the class
  • Muhammad Rizwan Munawar
    Muhammad Rizwan Munawar over 3 years
    sure, your got solution or not
  • gvd
    gvd over 2 years
    Note that when this could works, the solution given in upgrade 8.x guide in routing section laravel.com/docs/8.x/upgrade#routing is set to null $namespace property in RouteServiceProvider like is suggested bellow stackoverflow.com/questions/63845754/…