Laravel — P23: Route Groups

Efficiency Through Organization: Laravel’s Route Groups Explained

Who could have guessed that there would be so much to discuss in routing. If you think this is a lot, there will be another 10 articles or so on just routing a little later when we get into more advanced stuff. We’re still baby-stepping through Laravel. I’m eager to get us into Controllers and Models, so we’ll do an intro into those next.

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');
If we wanted to get to it using our name, we need to use our 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');
What if we wanted to repeat the process for the rest of our routes? We can prefix the 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');
});
Just remember that our route will require an argument to be passed to it.
{{ route('admin.marketing', ['Dino']) }}
That’s enough of routing for now. We’ll continue the discussion later. Let’s discuss Controllers next.

Laravel Series

Continue your Laravel Learning.

Laravel — P22: Named Routes

Unlocking the Power of Laravel: Mastering Named Routes

Laravel – P22: Named Routes

Elevate your Laravel projects with named routes for cleaner code and easier navigation. Discover the essentials in our guide.

Laravel — P23: Route Groups

Efficiency Through Organization: Laravel’s Route Groups Explained

Laravel – P23: Route Groups

Streamline your Laravel app with route groups for better structure & maintenance. Learn how to organize routes effectively.

Laravel — P24- Controllers Intro

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.

Leave a Reply