Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

I was reading laravel 5.1 documentation. I didn't understand how laravel route group working and what's the difference between following route groups.

Route Groups & Named Routes

If you are using route groups, you may specify an as keyword in the route group attribute array, allowing 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 may use the middleware key in the group attribute array. Middleware will be executed in the order 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-case for route groups is assigning the same PHP namespace to a group of controllers. You may 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
        });
    });

Sub-Domain Routing

Route groups may also be used to route wildcard sub-domains. Sub-domains may be assigned route parameters just like route URIs, allowing you to capture a portion of the sub-domain for usage in your route or controller. The sub-domain may be specified using the domain key on the group attribute array:

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

Route Prefixes

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

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

You may 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
    });
});

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

like image 310
scott Avatar asked Aug 04 '15 04:08

scott


1 Answers

Route groups allow you to group together routes that share common attributes without having to redefine said attributes for each route.

Example

As an example lets use the namespace array attribute.

Say we have a controller called NewsController that contains all the Admin logic for your apps news section. You may place this file within the 'App/Http/Controllers/Admin` directory.

Laravel 5 follows PSR-4 autoloading conventions, so the application expcets the namespace to be identical to the path of the file, so our class might look something like this:

<?php

namespace App\Http\Controllers\Admin;

class NewsController
{

}

We could write a route to this class like so:

Route::get('admin/news', [
    'uses' => 'Admin\NewsController@index'
]);

Note: Laravel automatically assumes all your controllers will be in the App/Http/Controllers directory so we can leave this out of any controller declarations in the routes file.

The above should work fine, but maybe you also have a dozen or so other class files that deal with Admin logic all within the same namespace. We can use the namespace option to group these together.

Route::group(['namespace' => 'Admin'], function()
{
    Route::get('admin/news', [
        'uses' => 'NewsController@index'
    ]);

    Route::get('admin/users', [
        'uses' => 'UserController@index'
    ]);

    ...
});

Notice how I no longer define the Admin namespace for the controller for each route.

The same process can be applied to middleware, subdomains, and url prefixes.

Further Example

Lets take the first example and build on it. As you can see from the above route declarations 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 define the common url for our routes. In our case this is admin.

Our updated Route declarations would look like so.

Route::group(['namespace' => 'Admin', 'prefix' => 'admin'], function()
{
    Route::get('news', [
        'uses' => 'NewsController@index'
    ]);

    Route::get('users', [
        'uses' => 'UserController@index'
    ]);

    ...
});

Your probably wondering why would this be useful? Well imagine you have developed a large application with tens if not hundreds of routes. Then one day your boss comes to you and says "Hey Mr. tester, we need to change the admin url from /admin to /cms, how long will this take?".

If you've declared all your routes using groups with the prefix array attribute like above, this is going to be an easy and painless process for you.

like image 179
Jeemusu Avatar answered Oct 02 '22 19:10

Jeemusu