OneBite.Dev

Target class controller not found laravel

Here is How to solve error Target class controller not found in Laravel project

Target class [$NameController] does not exist.

It caused by newer Laravel (>L8) didn’t namespace the controller by default.

Solution 1

If you want to make it works like it used to

Go to RouteServiceProvider file add namespace variable and namespace method in both api and web

...
protected $namespace = 'App\Http\Controllers';  // <- add this variable
...

 public function boot(): void
    {
        RateLimiter::for('api', function (Request $request) {
            return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
        });

        $this->routes(function () {
            Route::middleware('api')
                ->prefix('api')
                ->namespace($this->namespace) // <- add this
                ->group(base_path('routes/api.php'));

            Route::middleware('web')
                ->namespace($this->namespace)  // <- add this
                ->group(base_path('routes/web.php'));
        });
    }

Solution 2

If you only need it once, you can just add namespace at your controller when including it in routes example

Route::get('/how/{unit}-to-{unit2}', 'App\Http\Controllers\ConverterController@convert');
laravel