How to fix Target class does not exist in Laravel 8

  409 views   1 month ago Laravel

Laravel 8 has many new features and changes. so, today i create one laravel 8 application and try to make a simple CRUD application then i get the "Target class does not exist" error when I try to call the controller's index method. then i read the laravel 8 document and resolve the "How to fix Target class not exist" error in my laravel 8 application. so, i also share with you how  to fixed target class does not exist in your laravel 8 application.

this this screenshot

actually this is not but in laravel 8 remove the default namespace from the "app/Providers/RouteServiceProvider.php" file.

so, if we want to use any controller in laravel 8 "web.php" route file then we simply first use from the top of the file. see the code.

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;
  
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
  
Route::get('/', function () {
    return view('welcome');
});

// For resource route.
Route::resource('products', PostController::class);
// if you want call any single method of controller
Route::get('products', [PostController::class, 'index'])->name('products.index');

but if you don't want all the "Controller" file every time then simply change your "app/Providers/RouteServiceProvider.php" file like that and resolve the issue from your laravel 8 application.

namespace App\Providers;
  
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;
  
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
     */
    public const HOME = '/home';
    protected $namespace = 'App\Http\Controllers';
  
    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot()
    {
        $this->configureRateLimiting();
  
        $this->routes(function () {
            Route::middleware('web')
                ->namespace($this->namespace)
                ->group(base_path('routes/web.php'));
  
            Route::prefix('api')
                ->middleware('api')
                ->group(base_path('routes/api.php'));
        });
    }
  
    ....

after change this then your "web.php" route file looks clean.

use Illuminate\Support\Facades\Route;
  
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
  
Route::get('/', function () {
    return view('welcome');
});

// For resource route.
Route::resource('products', PostController::class);
// if you want call any single method of controller
Route::get('products', [PostController::class, 'index'])->name('products.index');

i hope you like this solution.

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on harsukh21@gmail.com

Related Articles