Laravel route groups allow you to organize routes that share attributes, such as path, name, or middleware. This will enable you to avoid defining these attributes on each route.
If you use route groups, you do not have to apply the attributes individually to each route; this avoids duplication.
For example, if you want to apply a middleware or controller to different routes, you can create a route group and then use the controller or middleware for that group.
Here are five parameters you can use to define a group in Laravel:
- Middleware
- Controllers
- Route prefixes
- Route name prefixes
- Subdomain routing
Middleware
You can assign a middleware to a group of routes. Middlewares are executed in the order they are listed in the array.
To assign a middleware to a specific group of routes, use the middleware() method before defining a group() like this:
<?php use Illuminate\Support\Facades\Route; use App\Http\Middleware\Admin; use App\Http\Middleware\Sudo; Route::middleware(['Admin', 'Sudo'])->group(function () { Route::get('/admin', function () { // your code }); Route::get('/admin/profile', function () { // your code }); });
In this code, we define two middlewares, “Admin” and “Sudo,” for the “/admin” and “/admin/profile” routes. That means these routes are now protected; only admin or sudo users can access them.
Controllers
If you have a usecase using multiple methods under a single controller, then this type of grouping is beneficial.
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\ProductController; Route::controller(ProductController::class)->group(function () { Route::get('/product/create', 'create'); Route::post('/product/store', 'store'); });
We imported a ProductController file in this code and defined two routes called /product/create and /product/store under the “ProductController” group.
Route Prefixes
If you have multiple routes which have the same prefix, then you can group all of your routes based on specific prefixes like this:
<?php use Illuminate\Support\Facades\Route; Route::prefix('/product')->group(function () { Route::get('/create', function () { // Matches The "/product/create" URL }); Route::get('/edit/{id}', function () { // Matches The "/product/edit/{id}" URL }); });
In this code, we are grouping multiple routes based on the “/product” prefix. That means our URI looks like this: /product/create and /product/edit/1
Route Name Prefixes
You can use the name() method to prefix each route name in the group with a given string. For example, you may want to name all your routes starting as “product.” and then add your specific route name like this:
Route::name('product.')->group(function () { Route::get('/create', function () { // Route assigned name "product.create"... })->name('create'); });
You can see that the string we provided is prefixed to the route name exactly as specified, so we gave the trailing . character in the prefix.
Subdomain Routing
If your application utilizes subdomains, you can handle your routing system using “subdomain routing”.
The subdomain may be specified by calling the domain method before defining the group like this:
Route::domain('{forum}.appdividend.com')->group(function () { Route::get('user/{id}', function (string $forum, string $id) { // your code }); });
To ensure your subdomain routes are reachable, you should register subdomain routes before registering root domain routes.
That’s all!
Hamza Hameed
Thanks for sharing this article. I have read many articles related to route group in laravel 8 but you teach route group very well. Keep it up