Aakash Limbani
Aakash Limbani

Reputation: 153

Target class controller [controllerName] not found in Laravel 8

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:

Upvotes: 0

Views: 481

Answers (3)

meyegui
meyegui

Reputation: 169

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

    // ...
}

Upvotes: 1

Dip Ghosh
Dip Ghosh

Reputation: 585

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

Upvotes: 0

user7833368
user7833368

Reputation:

It should be changed to:

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

Upvotes: 0

Related Questions