Laravel Route Groups [Step-by-step Guide]

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.

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

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

Step 1: Define the 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');
});

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 a namespace, prefix, or middleware for the group of routes. Save the file and go to http://localhost:8000/users. You will see `Yes!! Admin namespace is working successfully. 

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 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 a 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');
});

Your URL should be http://localhost:8000/admin/users and not http://localhost:8000/users.

See also

Laravel CRUD

1 thought on “Laravel Route Groups [Step-by-step 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.