I have a fresh installation of Laravel 9 and I tried to uncomment controller namespace in RouteServiceProvider.php. But in my api routes throw an error:
Undefined class 'MainController'
My controller is correctly placed under this namespace.
App\Http\Controllers
api.php file is like this.
Route::group(['prefix' => '/main'], function () {
Route::get('/', [MainController::class, 'index']);
});
Controller file is like this.
<?php
namespace App\Http\Controllers;
class MainController extends Controller
{
public function index()
{
return response()->json(['status'=>200,'message'=>'success']);
}
}
If I import the controller file to api routes file, it works as normal.
CodePudding user response:
use this in route file web.php
use App\Http\Controllers\MainController;
Route::get('/', [MainController::class, 'index']);
CodePudding user response:
You have to add full path of use controller in route file like these
use App\Http\Controllers\MainController;
And also use full path when we declare a route...
Route::get('/', [App\Http\Controllers\MainController::class, 'index']); });