Laravel 8 Route Groups: Complete Guide

 Routes Groups are helpful in the situation when you want to apply one attribute to all the routes.

Laravel 8 Route Groups

Laravel 8 route groups allow you to group all the routes related to your modules. Route group takes an array that can take attributes and callback function. The route group allows you to share attributes such as middleware, prefixed, or namespaces without defining these attributes on each route.

These shared attributes can be passed in the array format as the first argument to the Route::group() function.

For example, if you want to apply a namespace to different routes, you can create a route group and then use the namespace for that group.

Syntax

Route::group( [ ] , callback);

Parameters

The group() function takes an array passed to the group method as a first parameter. The second parameter is the callback function.

Define Route Groups in Laravel 8

Before defining route groups, let’s create a namespace for our project.

Step 1: Define namespace.

Laravel namespaces are defined as a class of elements in which each element has a unique name to the associated class.

To create a custom namespace in Laravel, create a separate controller with forward-slash(/) using the following command.

php artisan make:controller Admin/UserController --resource --model=User

The UserController will be created inside the Admin directory inside the app >> Http >> Controllers folder.

Add the following code inside the UserController’s index() method.

// UserController.php

public function index()
{
        return 'Yes!! Admin namespace is working successfully';
}

Step 2: Define group() function.

To write Admin namespace routes, open routes >> web.php file and add the following code.

// web.php

Route::namespace('Admin')->group(function() {
    Route::resource('users', 'UserController');
});

Here, we assign a namespace Admin to the route users. You can see here that we have grouped the routes using the group() method. We can also write this code better, like the following.

// web.php

Route::group(['namespace' => 'Admin'], function() {
    Route::resource('users', 'UserController');
});

Here, we have used the group() function, which takes two parameters, as mentioned in the syntax.

  1. array
  2. callback function

The first parameter is an associative array containing namespace, prefix, or middleware for the group of routes. Save the file and go to the http://localhost:8000/users. You will see `Yes!! Admin namespace is working successfully`. 

Here, you can add as many routes as possible, categorized under the Admin namespace.

// web.php

Route::group(['namespace' => 'Admin'], function() {
    Route::resource('users', 'UserController');
    Route::resource('sales', 'SalesController');
    Route::resource('marketings', 'MarketingController');
});

Laravel 8 Path Prefixes

Path prefix is useful when we want to provide a standard URL structure. For example, in our application, we are creating functionality for the admin module which means we can create a standard prefix admin for all of our routes under admin functionality.

To add path prefix, add one more item in the associative array parameter of the route group called prefix as key and admin as value.

// web.php

Route::group(['namespace' => 'Admin', 'prefix' => 'admin'], function() {
    Route::resource('users', 'UserController');
});

Now, your URL should be http://localhost:8000/admin/users and not http://localhost:8000/users.

Laravel 8 Middleware

Middleware provides a convenient mechanism for filtering HTTP requests entering your application. We can also assign middleware to all the routes within a group. We can add one more property to the associative array whose key is middleware and value is ‘middleware name’. 

In this example, we will use auth middleware, so type the following command in your terminal to scaffold the auth module provided by Laravel.

php artisan ui:auth

Now we want to protect the admin routes so that we will use auth middleware for admin routes. To do that, write the following code inside the web.php file.

// web.php

Route::group(['namespace' => 'Admin', 
            'prefix' => 'admin',
            'middleware' => 'auth'], function() {
    Route::resource('users', 'UserController');
});

Now, our admin routes are protected, and if you are logged in to the application, you will be able to see the content. Otherwise, you will be redirected to the login page.

Go to this URL: http://localhost:8000/admin/users, and you will be redirected to the http://localhost:8000/ login page. So this is how you can multiple routes and assign them to a common group of the route which shares the namespace, prefix, and middleware.

See also

Laravel 8 CRUD

Laravel 8 Route Macros

1 thought on “Laravel 8 Route Groups: Complete Guide”

  1. 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

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.