Target class [HomeController] does not exist
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.phpuse 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.phpprotected $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')); }); }
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
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.
Random Code Snippet Queries: Laravel
- Recursive function example code PHP Laravel
- Display message with session flash using bootstrap alert class in laravel
- How to add columns in existing table using migration in laravel
- How to restore deleted records in laravel
- Get posts belongs to a specific user in Laravel
- Laravel recursive function in controller
- Json encode method in laravel
- Get domain name in laravel
- How to fill a column automatically while creating records in Laravel
- After image selected get validation error in laravel
- Get last year created records in Laravel
- Command to create MySQL Docker image and access the MySQL command-line interface (CLI) within a running Docker container
- Retrieve count of nested relationship data in Laravel
- Composer create project laravel/laravel example app
- How to get tomorrow and yesterday date in laravel
- Syntax error or access violation: 1072 Key column 'role_id' doesn't exist in table (SQL: alter table `users` add constraint `users_role_id_foreign` foreign key (`role_id`) references `roles` (`id`))
- Validation errors for multiple forms on same page Laravel
- Class 'App\Providers\Auth' not found
- Credit card validation in laravel
- Define variable and use in Laravel controller method
- How to create pivot table in laravel using migration
- Laravel save object to database
- How to get random string in Laravel
- How to upload image in laravel 8
- Ajax POST request in laravel