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
- Update email with unique validation in laravel
- Count all and get 10 records after where condition in laravel
- Validation for multiple forms on same page in laravel
- How to display user profile after login in laravel
- How to start websocket server in laravel
- First and last item of the array using foreach iteration in laravel blade
- File_put_contents(/var/www/html/w3code/storage/framework/sessions/CXwN3EXKxERD6jgy3rADcaAAbAx8FRKih2JK7UO9): Failed to open stream: Permission denied
- Get current month records in laravel 7/8
- How to insert multiple rows in mysql using loop in laravel?
- How to get all posts which contains comments in laravel
- Get the products list ordered by a user
- Update last created record in Laravel
- Get only 10 records from table in laravel
- Skip first n record and display rest records in laravel view
- Laravel recursive function in controller
- How to create controller in laravel
- Laravel get all records with pagination
- Laravel onclick function not working
- Print query in laravel
- How to restore multiple records after soft-deletes in Laravel
- Insert Comma Separated Values in laravel
- How to create projects method with belongstomany relationship in user model
- Add a subselect based on relationship using withAggregate method
- Validation errors for multiple forms on same page Laravel
- Retrieve count of nested relationship data in Laravel