[Solved] Target class Controller does not exist

“Target class Controller does not exist” issue comes in Laravel 8. One simple trick can solve this issue.

This error comes in Laravel new version because there is no namespace prefix being applied to your route groups that your routes are loaded into. In the old version of Laravel, the RouteServiceProvider contained a $namespace property. In the old version property’s value would automatically be prefixed onto the controller route.

So to solve this issue 

We need to Fully Qualify Class Name for your Controllers which is you are going to use. Or if you are using the class name at the top you must be used the namespace prefix.

use App\Http\Controllers\PageController;

Route::get('/page', [PageController::class, 'index']);
// or
Route::get('/page', 'App\Http\Controllers\PageController@index');

Another way to solve 

Define namespace in RouteServiceProvider as an old version. 

App\Providers\RouteServiceProvider

public function boot()
    {
        $this->configureRateLimiting();

        $this->routes(function () {
            Route::prefix('api')
                ->middleware('api')
                ->namespace($this->namespace)
                ->namespace('App\Http\Controllers')  <------------ Add this
                ->group(base_path('routes/api.php'));

            Route::middleware('web')
                ->namespace($this->namespace)
                ->namespace('App\Http\Controllers') <------------- Add this
                ->group(base_path('routes/web.php'));
        });
    }

So here we solve this issue Target class Controller does not exist in Laravel 8.

Comments are closed.