Error

“Target class Controller does not exist” issue comes in Laravel 8. One simple trick can solve this issue.
The reason for this problem in the latest version of Laravel is that the route groups into which your routes are loaded are not receiving the namespace prefix. The $namespace property was present in the RouteServiceProvider in the previous iteration of Laravel. The property’s value would have automatically prefixed the controller route in the previous version.
So to solve this issue
- So to solve this issue
- Another way to solve
We must fully qualify the class name that you intend to use for your controllers. Alternatively, you have to use the namespace prefix if you are using the class name at the top.
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'));
});
}
Leave a Reply