Difference between middleware route group and namespaces route group in laravel 5.1?

I read the laravel 5.1 documentation. I did not understand how the laravel route group works and what is the difference between the following route groups.

Route Groups and Named Routes

If you use route groups, you can specify the as keyword in the array of attributes of the route group, which allows you to set a common route name prefix for all routes within the group:

Route::group(['as' => 'admin::'], function () { Route::get('dashboard', ['as' => 'dashboard', function () { // Route named "admin::dashboard" }]); }); 

Middleware

To assign middleware to all routes within a group, you can use the middleware key in the group attribute array. Middleware will run in the order in which you define this array:

 Route::group(['middleware' => 'auth'], function () { Route::get('/', function () { // Uses Auth Middleware }); Route::get('user/profile', function () { // Uses Auth Middleware }); }); 

Namespaces

Another common use for route groups is to assign the same PHP namespace to a group of controllers. You can use the namespace parameter in your group attribute array to specify the namespace for all controllers within the group:

  Route::group(['namespace' => 'Admin'], function() { // Controllers Within The "App\Http\Controllers\Admin" Namespace Route::group(['namespace' => 'User'], function() { // Controllers Within The "App\Http\Controllers\Admin\User" Namespace }); }); 

Subdomain Routing

Route groups can also be used to route wildcard subdomains. Subdomains can be assigned route parameters similar to route URIs, allowing you to capture part of the subdomain for use in your route or controller. A subdomain can be specified using a domain key in an array of group attributes:

 Route::group(['domain' => '{account}.myapp.com'], function () { Route::get('user/{id}', function ($account, $id) { // }); }); 

Route prefix

The prefix array attribute can be used to prefix each route in a group with a given URI. For example, you can prefix all route URIs within a group with admin:

 Route::group(['prefix' => 'admin'], function () { Route::get('users', function () { // Matches The "/admin/users" URL }); }); 

You can also use the prefix parameter to specify common parameters for your grouped routes:

 Route::group(['prefix' => 'accounts/{account_id}'], function () { Route::get('detail', function ($account_id) { // Matches The accounts/{account_id}/detail URL }); }); 

Link: http://laravel.com/docs/5.1/routing

+8
php
source share
1 answer

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.

+9
source share