What are Laravel route groups?

Route groups let you organize routes that share attributes, such as path, name, or middleware. This allows you to avoid having to define these attributes on each individual route.

Creating a basic route group

A basic route group in Laravel is created using Route::group(), as shown below.

Route::group([
// All parameters go here
], function () {
// All routes go here
});

The method takes in two parameters, an array, and a callback. The array contains all shared features of the routes in the group. An example can be seen below.

Route::group([
'prefix' => '/admin',
'as' => 'admin.',
], function () {
// All routes go here
});

Here, all the routes in this group are prefixed with the /admin path and also prefixed with the admin name. The callback contains all routes in the group. So, if a route is defined like this:

Route::group([
'prefix' => '/admin',
'as' => 'admin.',
], function () {
Route::get('/', [AdminController::class, 'index'])->name('index');
});

The route actually refers to the /admin/ path and can be accessed with the name “admin.index”. For as many routes as you can include in the group, each one inherits these rules.

New on Educative
Learn any Language for FREE all September 🎉
For the entire month of September, get unlimited access to our entire catalog of beginner coding resources.
🎁 G i v e a w a y
30 Days of Code
Complete Educative’s daily coding challenge every day in September, and win exciting Prizes.

Free Resources

Attributions:
  1. undefined by undefined