Target class does not exist. problem in laravel 8 [duplicate]

Laravel 8 Update the way to write routes

ref link https://laravel.com/docs/8.x/upgrade

in laravel 8 you need to use like

use App\Http\Controllers\SayhelloController;
Route::get('/users/{name?}' , [SayhelloController::class,'index']);

or

Route::get("https://stackoverflow.com/users", 'App\Http\Controllers\UserController@index');

If you want to use old way

then in RouteServiceProvider.php

add this line

 /**
     * This namespace is applied to your controller routes.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $namespace="App\Http\Controllers"; // need to add in Laravel 8
    

public function boot()
{
    $this->configureRateLimiting();

    $this->routes(function () {
        Route::prefix('api')
            ->middleware('api')
            ->namespace($this->namespace) // need to add in Laravel 8
            ->group(base_path('routes/api.php'));

        Route::middleware('web')
            ->namespace($this->namespace) // need to add in Laravel 8
            ->group(base_path('routes/web.php'));
    });
}

Then you can use like

Route::get('/users/{name?}' , [SayhelloController::class,'index']);
Route::resource("https://stackoverflow.com/users" , SayhelloController::class);

or

Route::get("https://stackoverflow.com/users", 'UserController@index');

Leave a Comment