Efficiency Through Organization: Laravel’s Route Groups Explained
What are route groups?
Route groups is a nice and simple way to reduce redundancy. Let’s say that we have the following routes:
/admin/contact
/admin/dashboard
/admin/sales
/admin/marketing
You may see a pattern in the routes above. That pattern is that each route has the word admin
in it. Route Groups allow us to create an outside function that encompasses the other routes. Route groups allow for us to share attributes amongst routes as well. If we need to pass a specific key or bind some middleware to each route, we can use a group and bind it to the entire group. Let’s see some examples with route groups.
Route Prefix
The first example that we’ll look at is removing the admin
from each route and prefixing it to the group. You’ll need to use the prefix
function and chain on the group
function after it. The group
function accepts a closure that contains all of our routes. As you can see, each of the routes has been stripped of the admin
string and has been moved up to the group level.
<?php
Route::prefix('admin')->group(function() {
Route::get('/contact', function() {
return "Admin - Contact";
});
Route::get('/dashboard', function() {
return "Admin - Dashboard";
});
Route::get('/sales', function() {
return "Admin - Sales";
});
Route::get('/marketing', function() {
return "Admin - Marketing";
});
});
To visit your route, you will still add admin
in front of the selected location, i.e. http://0.0.0.0/admin/sales
.
We can add URL parameters to our prefix
and send it through the group closure. Know that this changes the URL structure for all routes inside the closure.
<?php
Route::prefix('admin/{name}')->group(function($name) {
Route::get('/contact', function($name) {
return "Admin - Contact " . $name;
});
Route::get('/dashboard', function() {
return "Admin - Dashboard";
});
Route::get('/sales', function() {
return "Admin - Sales";
});
Route::get('/marketing', function() {
return "Admin - Marketing";
});
});
You can visit the /contact
route by typing in the following: http://0.0.0.0/admin/Dino/contact
.
Route Name Prefixes
Remember our named routes from the previous article? We can prefix our named routes with the name
prefix.
We have the following route:
<?php
Route::get('/marketing', function() {
return "Admin - Marketing";
})->name('marketing');
route
function inside of our Blade file: {{ route('marketing') }}
.What if we wanted to use {{ route('admin.marketing') }}
instead? We could just append admin.
to our route.
<?php
Route::get('/marketing', function() {
return "Admin - Marketing";
})->name('admin.marketing');
name
and chain on the group
function. Make sure to pass admin.
with the period.<?php
Route::name('admin.')->group(function($name) {
Route::get('/marketing', function() {
return "Admin - Marketing";
})->name('marketing');
});
Can we use our route prefix
function and the name
function in the same grouping? Yes. Just keep chaining on.
<?php
Route::prefix('admin/{name}')->name('admin.')->group(function($name) {
Route::get('/contact', function($name) {
return "Admin - Contact " . $name;
})->name('contact');
Route::get('/dashboard', function() {
return "Admin - Dashboard";
})->name('dashboard');
Route::get('/sales', function() {
return "Admin - Sales";
})->name('sales');
Route::get('/marketing', function() {
return "Admin - Marketing";
})->name('marketing');
});
{{ route('admin.marketing', ['Dino']) }}
Laravel Series
Continue your Laravel Learning.
Unlocking the Power of Laravel: Mastering Named Routes
Elevate your Laravel projects with named routes for cleaner code and easier navigation. Discover the essentials in our guide.
Efficiency Through Organization: Laravel’s Route Groups Explained
Streamline your Laravel app with route groups for better structure & maintenance. Learn how to organize routes effectively.
Navigating Laravel: An Introduction to Controllers
Laravel – P24: Controllers Intro
Begin your journey with Laravel controllers to manage app logic & data flow. Discover tips for effective MVC architecture integration.