Laravel Route Group Example

One of the most important features of Laravel is its routing system, which allows developers to define how URLs are mapped to specific actions in their application. Laravel route groups provide a powerful way to group routes together and apply common middleware, route parameters, or other options to all routes in the group. In this article, we will explore Laravel route groups with examples.

Laravel route groups allow developers to group routes together based on a common trait. For example, if you have a set of routes that are only accessible to authenticated users, you can group them together using route groups. You can also apply middleware to the route group to ensure that all routes within the group are protected by the same middleware.

Route groups can be defined using the Route::group method. The method takes two arguments: an array of options and a closure function that defines the routes within the group. The options array can include middleware, prefixes, and namespaces for the group. The closure function defines the routes within the group, using the same syntax as individual route definitions.

Laravel Route Group Example

Group routing is done in the following way in Laravel application. which are mentioned below:

  • Defining a Route Group in Laravel
  • Applying Middleware to a Route Group
  • Applying Route Parameters to a Route Group

Defining a Route Group in Laravel

A route group in Laravel is defined using the Route::group() method. This method takes a callback function that defines the routes in the group, along with any middleware or other options that should be applied to all routes in the group.

Here is a basic example of a route group in Laravel:

Route::group(['prefix' => 'admin'], function () {
    Route::get('/', 'AdminController@index');
    Route::get('/users', 'AdminController@users');
    Route::get('/settings', 'AdminController@settings');
});

In this example, we are defining a route group with the prefix “admin”. This means that all routes in the group will have the URL prefix “/admin”. Within the group, we define three routes using the Route::get() method. Each route maps a URL to a controller action.

Applying Middleware to a Route Group

One of the main benefits of using a route group in Laravel is the ability to apply middleware to all routes in the group. Middleware is code that is executed before or after a route handler. It can be used for tasks like authentication, logging, or input validation.

To apply middleware to a route group in Laravel, we use the middleware() method within the Route::group() callback. This method takes one or more middleware class names as arguments.

Here is an example of a route group with middleware applied:

Route::group(['middleware' => 'auth'], function () {
    Route::get('/', 'DashboardController@index');
    Route::get('/profile', 'UserController@profile');
    Route::post('/update-profile', 'UserController@updateProfile');
});

In this example, we are applying the “auth” middleware to all routes in the group. This means that before any of the routes are executed, the middleware will check if the user is authenticated. If the user is not authenticated, they will be redirected to the login page.

Applying Route Parameters to a Route Group

Another useful feature of Laravel route groups is the ability to apply route parameters to all routes in the group. Route parameters are placeholders in a URL that can be used to capture dynamic values, like an ID or a username.

To apply route parameters to a route group in Laravel, we use the where() method within the Route::group() callback. This method takes an array of parameter names and regular expressions that define the allowed values for each parameter.

Here is an example of a route group with route parameters applied:

Route::group(['prefix' => 'users/{user_id}', 'where' => ['user_id' => '[0-9]+']], function () {
    Route::get('/', 'UserController@index');
    Route::get('/profile', 'UserController@profile');
    Route::post('/update-profile', 'UserController@updateProfile');
});

In this example, we are defining a route group with the prefix “users/{user_id}”. This means that all routes in the group will have a URL that starts with “/users/” and includes a dynamic parameter named “user_id”. We are also using the where() method to specify that the “user_id” parameter must be a number.

Conclusion

Laravel route groups are a powerful tool that can be used to group related routes together and apply middleware to them. By using route groups, you can make your routing system more efficient and easier to manage. In this article, we explored an example of how Laravel route groups can be used to define a web application with public and admin sections. By using route groups, we were able to ensure that only authenticated users with the “admin” role can access the admin section of the application, while allowing all users to access the public section.

More Tutorials

Leave a Comment