
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
- How to use more than one query scope in Laravel
- Call to undefined function Illuminate\Encryption\openssl_cipher_iv_length()
- Update existing pivot table data in laravel
- The POST method is not supported for this route. Supported methods: PUT.
- Get duplicate records in laravel
- How to get last record from object collection in laravel
- How to get selected categories on edit record with Select2
- Convert input array to comma-separated string in laravel controller
- Ajax GET request in laravel
- Credit card validation in laravel
- How to get records in random order in laravel
- Laravel order by date not working
- SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel8.projects' doesn't exist
- Global scope in Laravel with example
- How to pass data to partial view file in laravel
- Array to string conversion laravel Controller
- How to get session in blade Laravel ?
- How to authenticate admin users in Laravel ?
- How to call controller function from view in Laravel
- Database transactions in laravel
- How to upload multiple images after preview in laravel using cropper js
- Argument 1 passed to Illuminate\Database\Query\Builder::cleanBindings() must be of the type array, null given
- How to restore multiple records after soft-deletes in Laravel
- Send OTP using textlocal api in laravel
- Send id with route Laravel