Search code examples
laravelframeworksbackendlaravel-routinglaravel-8

Route group namespace in Laravel 8


Im trying to defining namespace in laravel

old ver

Route::group('namespace' => 'RoleA'], function() {
    Route::get('/', 'HomeController@index']);
    // call RoleA/HomeController
});

laravel 8

use App\Http\Controllers\RoleA\HomeController;
use App\Http\Controllers\RoleB\HomeController;
// return error => HomeController already in use

Route::group('namespace' => 'RoleA'], function() {
    Route::get('/', [HomeController::class, 'index']);
    // call RoleA/HomeController
});

Route::group('namespace' => 'RoleB'], function() {
    Route::get('/', [HomeController::class, 'index']);
    // still call RoleA/HomeController
});

is there another way or a correct way to use namespace in Laravel 8?

Edit, sorry for the ambigous question

I mean like the old version, when defining namespace similar like this, or see the old version above

Route::group('namespace' => 'RoleB'], function() {
    Route::get('/', [RoleB/HomeController::class, 'index']);
    // will call RoleB/HomeController
});

is there a way to achieve something similar like code above?


Solution

  • I think laravel drop the feature, because in laravel 8, controller will be Object oriented class

    Laravel Routing 7.* Docs.

    Laravel Routing 7.* Docs.

    Laravel Routing 8.* Docs.

    Laravel Routing 8.* Docs.