Home > Software design >  Target class controller [controllerName] not found in Laravel 8
Target class controller [controllerName] not found in Laravel 8

Time:03-07

When I hit my register route using Postman, it gave me the following error:

My api.php route:
Route::get('register', 'Api\RegisterController@register');

Here is my controller:

CodePudding user response:

In the official Laravel 8 upgrade guide, you can see that controllers are no longer namespaced by default. This hinders the automatic class resolution with 'Controller@method' syntax.

See https://laravel.com/docs/8.x/upgrade#automatic-controller-namespace-prefixing

The new way of registering routes is to use the following syntax:

// routes/api.php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Api\RegisterController;

Route::get('register', [RegisterController::class, 'register');

You can also adapt your RouteServiceProvider.php file to "re-enable" the old way to auto-load your controllers using the correct namespace with @ syntax:

// App/Providers/RouteServiceProvider.php

public function boot()
{
    // ...

    Route::prefix('api')
        ->middleware('api')
        ->namespace('App\Http\Controllers') // put your namespace here
        ->group(base_path('routes/api.php'));

    // ...
}

CodePudding user response:

It should be changed to:

Route::get('register', [RegisterController::class, 'register']);

CodePudding user response:

first, in the route add the controller name.

use App\Http\Controllers\RegisterController;

In the route use like this way

Route::get('register', [RegisterController::class, 'register']);
  • Related