How to use grouping for Routes in Laravel

Categorized as Laravel Tagged

In this recent addition in Laravel (since version 8.8), you can use a route controller group instead of using the controller in each route in your routes/web.php file. See the example below:

// Before grouping
Route::get('contact', [PagesController::class, 'contact']);
Route::get('about', [PagesController::class, 'about']);
Route::get('/', [PagesController::class, 'index']);

// After grouping
Route::controller(PagesController::class)->group( function() {
  Route::get('contact', 'contact');
  Route::get('about', 'about');
  Route::get('/', 'index');
});

Enjoy!

Leave a reply

Your email address will not be published.