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.
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.
Free Resources