
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\[email protected]');
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 "[email protected]" 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', '[email protected]')->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
- Redirect to another view from controller in laravel
- How to insert value to additional columns in pivot table in laravel
- Get duplicate records in laravel
- How to check column value of a record is null or not in laravel
- Insert current date time in a column using Laravel
- Generate unique username in Laravel
- Input file with max size validation in laravel
- How to add foreign key in laravel using migration
- How to restore deleted records in laravel
- How to add dynamic page title in Laravel view
- Laravel migration add foreign key to existing table
- Display message with session flash using bootstrap alert class in laravel
- Attempt to read property "avatar" on null in Laravel
- Property [user] does not exist on this collection instance
- Laravel 9 route group with controller
- Update existing pivot table data in laravel
- How to get single column value in laravel
- How to get specific columns using with method in laravel Eloquent relationship
- How to add columns in existing table using migration in laravel
- Method Illuminate\Database\Eloquent\Collection::lists does not exist
- Create a record if not exist in laravel
- Laravel append URI in route
- How to send ID to another page in Laravel
- Json encode method in laravel
- Laravel file size validation not working