2

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.

CodeCanyon
  • 929
  • 2
  • 11
  • 36
  • Can you post your `routes/web.php` and controller code in your question? – aceraven777 Apr 26 '22 at 06:04
  • This might help you: https://stackoverflow.com/questions/66960224/controllers-not-working-on-laravel-8-despite-uncommenting-in-routeserviceprovide – aceraven777 Apr 26 '22 at 06:04
  • Maybe because in the controller you named it `MonitoringController` – aceraven777 Apr 26 '22 at 06:29
  • @aceraven777: Sorry I had pasted the wrong controller name. I updated it. So the controller name is correct in the route as MainController. – CodeCanyon Apr 26 '22 at 06:33
  • Please read the answer below by surbhi. in your routes file i think you need to put `use App\Http\Controllers\MainController;` at the top – aceraven777 Apr 26 '22 at 06:57
  • @renan-cuoghi asked: Your file name is the same as the controller class name? MainController.php? – flaxon Apr 26 '22 at 12:26

3 Answers3

2

It will still work just add the following to your RouteServiceProvider. It was removed from the latest version but you can add it back again.

/**
 * The controller namespace for the application.
 *
 * When present, controller route declarations will automatically be prefixed with this namespace.
 *
 * @var string|null
 */
protected $namespace = 'App\\Http\\Controllers';

Then make your boot method like the following.

/**
 * Define your route model bindings, pattern filters, and other route configuration.
 *
 * @return void
 */
public function boot()
{
    $this->configureRateLimiting();

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

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

That's all you need to do, then it will work again.

Benjamin
  • 133
  • 7
0

use this in route file web.php

use App\Http\Controllers\MainController;

Route::get('/', [MainController::class, 'index']);

  • I had added like this and it worked. But my requirement is without importing the controller files in the route file, and just enable it in the RouteServiceProvider class. In previous Laravel versions, it was possible. – CodeCanyon Apr 27 '22 at 03:12
0

If you replace this code in your RouteServiceProvider.php

protected $namespace = 'App\\Http\\Controllers';
  
  public function boot()
    {
        $this->configureRateLimiting();
    
        $this->routes(function () {
            Route::middleware('api')
                ->prefix('api')
                ->namespace($this->namespace)
                ->group(base_path('routes/api.php'));
    
            Route::middleware('web')
                ->namespace($this->namespace)
                ->group(base_path('routes/web.php'));
        });
    }

You have use the old way in your web.php, like so:

Route::group(['prefix'=>'/', 'namespace'=>'Frontend'], function(){
   Route::get('/', 'FrontendController@getIndex');
});

Then sould work

Deimos
  • 269
  • 1
  • 6
  • 17