Target class [HomeController] does not exist

Created at 08-Mar-2021 , By samar

Target class [HomeController] does not exist

With this article, we will examine several different instances of how to solve the "Target class [HomeController] does not exist".

  • Import controller and define route in web.php in laravel 8

    --PATH routes\web.php
    use App\Http\Controllers\HomeController;
    Route::get('/home', [HomeController::class, 'index'])->name('home');
    
    // or
    
    Route::get('/home', 'App\Http\Controllers\HomeController@index');
    

    As per the new version in laravel (laravel 8) first, you need to import the controller file in your web.php file and after that, you can call the controller method. Laravel 8 has changed the way of calling the controller and its method. You can't call the controller with the "HomeController@index" method.

  • Use old routing way using RouteServiceProvider.php in laravel 8

    --PATH app/providers/RouteServiceProvider.php
    <!-- Uncomment code from -->
    // protected $namespace = 'App\\Http\\Controllers';
    <!-- To --> 
    protected $namespace = 'App\\Http\\Controllers';
    

    If you want to use old routing ways (Route::get('/home', 'HomeController@index')->name('home');) which are used in laravel < = 7 version. You have to remove comment // protected $namespace = 'App\\Http\\Controllers'; from RouteServiceProvider.php file.

  • Use protected $namespace = 'App\\Http\\Controllers'; in app\Providers\RouteServiceProvider.php

    --PATH app\Providers\RouteServiceProvider.php
    protected $namespace = 'App\Http\Controllers';
    public function boot()
    {
        $this->configureRateLimiting();
    
        $this->routes(function () {
            Route::prefix('api')
                ->middleware('api')
                ->namespace($this->namespace)
                ->group(base_path('routes/api.php'));
    
            Route::middleware('web')
                ->namespace($this->namespace)
                ->group(base_path('routes/web.php'));
        });
    }
    

    Code after changes in app\Providers\RouteServiceProvider.php file

    class RouteServiceProvider extends ServiceProvider
    {
        /**
         * The path to the "home" route for your application.
         *
         * This is used by Laravel authentication to redirect users after login.
         *
         * @var string
         */
        protected $namespace = 'App\Http\Controllers';
        public const HOME = '/home';
    
        
    
        /**
         * Define your route model bindings, pattern filters, etc.
         *
         * @return void
         */
        public function boot()
        {
            $this->configureRateLimiting();
    
            $this->routes(function () {
                Route::prefix('api')
                    ->middleware('api')
                    ->namespace($this->namespace)
                    ->group(base_path('routes/api.php'));
    
                Route::middleware('web')
                    ->namespace($this->namespace)
                    ->group(base_path('routes/web.php'));
            });
        }

Back to code snippet queries related laravel

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.