Home > Software engineering >  Is there any better way to write my Laravel Routes by avoiding namespace repeaters
Is there any better way to write my Laravel Routes by avoiding namespace repeaters

Time:08-06

I try to clean my code api.php in Laravel 9.

I'm not sure if there is a better way to avoid repeating controller namespace. Here is my code:

Route::controller(App\Http\Controllers\Contacts\ContactController::class)->prefix('contacts')->group(function(){
        Route::get('/', [App\Http\Controllers\Contacts\ContactController::class, 'index']);
    });

Note : I want to groupe by Controller

CodePudding user response:

You're half way there. Route::controller permits you to write your routes in the group like this:

Route::get('/', 'index');

i.e.

Route::controller(App\Http\Controllers\Contacts\ContactController::class)->prefix('contacts')->group(function(){
    Route::get('/', 'index');
});

This will result in the index function of the group's controller being called.

  • Related