Route Groups is an essential feature in Laravel, which allows you to group all the routes. Routes Groups are beneficial when you want to apply the attributes to all the route
Here, we have used the group() function, which takes two parameters, as mentioned in the syntax.
1- array of attributes, string,Closure
2- callback function, string $routes
The first parameter is an associative array containing namespace, prefix, or middleware for the group of routes
The second paramter is a callback function which contain the list of routes inside the group.
Syntax:
Route::group( [ ] , callback);
Route::group( [ ] , function(){});
Group Without Naming:
Route::group([],function () {
Route::get('/', function () { });
});
Naming/Prefixing Group:
Route::group(['prefix'=>"name"],function () {
Route::get('/', function () { });
});
Route::group(['prefix'=>'users','as'=>'user.'], function(){
Route::get('/', [cotrollerName::class,"methodName"]);
});
Group with Middleware:
Route::group(['middleware' => 'auth'], function() {
Route::get('/dashboard', [cotrollerName::class,"methodName"]);
Route::resource('users', 'UserController');
});
Group with Namespace, Prefix, Middleware:
Route::group(['namespace' => 'Admin',
'prefix' => 'admin',
'middleware' => 'auth'], function() {
Route::resource('users', 'UserController');
});