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
- Sample .htaccess file and index.php file under public directory in laravel
- Send OTP using textlocal api in laravel
- SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key
- How to add background image to div using Tailwindcss, Vite in Laravel Environment
- Argument 1 passed to App\Http\Controllers\Auth\LoginController::authenticated() must be an instance of App\Http\Controllers\Auth\Request
- How to customize pagination view in laravel
- How to use more than one query scope in Laravel
- External link not working in laravel blade
- How to get laravel errors folder in views directory in laravel
- Where to use whereNotNull eloquent in laravel
- How to create static page in Laravel
- Count all and get 10 records after where condition in laravel
- How to upload multiple images after preview in laravel using cropper js
- Method Illuminate\Http\Request::validated does not exist
- SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'posts_user_id_foreign'; check that column/key exists
- How to pass data to route in laravel?
- Display first n record from collection in laravel view
- How to create laravel project using composer
- Extra Filter Query on Relationships in Laravel
- How to call model in blade laravel
- How to create pivot table in laravel using migration
- How to get date from created_at field in laravel
- Laravel create table migration with model
- Laravel 10 starter app using breeze on live server
- Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails