What are the Route Groups and its use in Laravel

Route Groups are an important feature in Laravel. It allows you to perform some action on all grouped URLs. Routes Groups are good if you have to apply some attributes or properties on the URLs.

It clubbed all the URLs in one and you can specify some rules and attributes to all at once, you do not need to apply attributes or rules to each route individually.

There are multiple attributes that we can apply on the Route Group method in Laravel for example Prefix, Middleware, Namespace, etc.

Syntax of Route Group Method

Route::group( [ ] , callback);  

Parameters

[ ]: It is an array passed to the group method as a first parameter. For example, you can pass Prefix, Middleware, Namespace, etc.

Lets understand from below Route Group example

Route::group(['prefix' => 'astrologer/v1','namespace' => 'AstrologerAppAPI', 'middleware' => ['staticAuth']], function () {
	Route::post('astrologerLogin', 'AstrologerAppController@astrologerLogin');
});

Let’s suppose our website URL is https://atcodex.com/. Since we have the prefix’ => ‘astrologer/v1 in the route group, inside of this group all the route will be prefixed with astrologer/v1

Then if we have to call the route, it will be like

https://atcodex.com/astrologer/v1/astrologerLogin

So no matter how many URLs you will put under this Route Group it will automatically add the prefix for them.

And same goes for the middleware and namespace. All properties of middleware and namespace applies to the URLs under this Route Group.

Basically its a grouping of URLs in one, and when you apply some attributes on that Route Group then it automatically gets applied on all the URL inside.