[Solved] Target class Controller does not exist.

Brijpal Sharma
laravel   Publish on:-September 30th, 2020 , Category:- Web Development


"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\[email protected]');

 

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.


Brijpal Sharma

Hello, My Name is Brijpal Sharma. I am a Web Developer, Professional Blogger and Digital Marketer from India. I am the founder of Codermen. I started this blog to help web developers & bloggers by providing easy and best tutorials, articles and offers for web developers and bloggers...


0 Comments

You must be logged in to post a comment.