[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. 


public function boot()

        $this->routes(function () {
                ->namespace('App\Http\Controllers')  <------------ Add this

                ->namespace('App\Http\Controllers') <------------- Add this

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

Comments are closed.