Route groups allow you to group routes that have common attributes without overriding the specified attributes for each route.
Example
An example is the namespace array attribute.
Say we have a controller called NewsController that contains all the admin logic for the news section of your applications. You can put this file in the directory "App / Http / Controllers / Admin".
Laravel 5 follows the PSR-4 startup rule , so the expesets namespace application is identical to the file path, so our class might look something like this:
<?php namespace App\Http\Controllers\Admin; class NewsController { }
We could write a route to this class as follows:
Route::get('admin/news', [ 'uses' => 'Admin\NewsController@index' ]);
Note : Laravel automatically assumes that all your controllers will be in the App/Http/Controllers directory so that we can leave this outside any controller declarations in the routes file.
The above should work fine, but maybe you also have a dozen other class files that are associated with admin logic within the same namespace. We can use the namespace option to group them together.
Route::group(['namespace' => 'Admin'], function() { Route::get('admin/news', [ 'uses' => 'NewsController@index' ]); Route::get('admin/users', [ 'uses' => 'UserController@index' ]); ... });
Note that I no longer define an Admin namespace for the controller for each route.
The same process can be applied to intermediate layers, subdomains, and URL prefixes.
Additional example
Let's look at the first example and build it. As you can see from the route descriptions above, all our admin routes share a common URL prefix.
http://example.com/ admin / news
http://example.com/ admin / users
We can use the prefix array attribute to determine the common URL for our routes. In our case, this is Admin .
Our updated Route ads will look like this.
Route::group(['namespace' => 'Admin', 'prefix' => 'admin'], function() { Route::get('news', [ 'uses' => 'NewsController@index' ]); Route::get('users', [ 'uses' => 'UserController@index' ]); ... });
You might be wondering why this would be helpful? Imagine that you have developed a large application with dozens, if not hundreds of routes. Then one day your boss comes to you and says: "Hi, Mr. Tester, we need to change the /admin URL from /admin to /cms , how long will it take?".
If you declared all your routes using groups with an attribute of the prefix array, as shown above, this will be a simple and painless process for you.