How to Solve Target Class Does Not Exist In Laravel

Hello Artisan

In this tutorial i will explain how we can solve target class does not exist in laravel 8. You know recently laravel 8 was released some days ago. They changed the routing system a bit from other s version of laravel.

In this tutorial we will solve laravel 8 routing problem like target class apphttpcontrollers does not exist. See the below code to solve this BindingResolutionException Target class does not exist.

Example code : 

use App\Http\Controllers\TestController;

Route::get('/', [TestController::class, 'index']);

 

So in Laravel 8 you have to show the path of your controller. You can use route groupe to avoid path as like below.


Route::namespace('App\Http\Controllers')->middleware('guest')->group(function () {
    Route::prefix('')->group(function () { //you can use prefix if you want
      Route::get('/', '[email protected]');
    });
});

 

Or if your are not happy with the above system and you would like to use it like laravel 7 then follow the below tips. go to your route service provider and update the code like below.

app/Providers/RouteServiceProvider.php

protected $namespace = 'App\Http\Controllers';

// the add this namespace like below

 $this->routes(function () {
            Route::middleware('web')
                ->namespace($this->namespace)
                ->group(base_path('routes/web.php'));

 

now you can simply use like laravel 7 or other version

Route::get('/', '[email protected]');

 

Hope it can help you.