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 () {
                ->namespace($this->namespace) // <- add this

                ->namespace($this->namespace)  // <- add this

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');