Middleware groups in Laravel 5.2

 

Laravel 5.2 has a new feature that allows you group several route middleware under a single name/key thus helping you assign several middleware to a route/group of routes at once.

Laravel 5.2 comes shipped with two major middleware groups named web and api.

Let’s take a look.

1. Open AppHttpKernel.php. You will see something like this:
    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
        ],

        'api' => [
            'throttle:60,1',
        ],
    ];

   
    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
        ],

        'api' => [
            'throttle:60,1',
        ],
    ];

2. Open routes.php, you will see something like this:
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| This route group applies the "web" middleware group to every route
| it contains. The "web" middleware group is defined in your HTTP
| kernel and includes session state, CSRF protection, and more.
|
*/

Route::group(['middleware' => ['web']], function () {
    //
});

Route::group(['middleware' => 'web'], function () {
    Route::auth();

    Route::get('/home', 'HomeController@index');
});
   
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| This route group applies the "web" middleware group to every route
| it contains. The "web" middleware group is defined in your HTTP
| kernel and includes session state, CSRF protection, and more.
|
*/

Route::group(['middleware' => ['web']], function () {
    //
});

Route::group(['middleware' => 'web'], function () {
    Route::auth();

    Route::get('/home', 'HomeController@index');
});

Every route you stuff inside this Route group will have all the middleware grouped under web have effect on it.

Also, for your API routes, you can also just apply the api middleware group like so:
Route::group(['middleware' => ['api']], function() {

   Route::get('/api/vi/songs', 'SongController@index');
   Route::get('/api/vi/song/{id}', 'SongController@fetchData');

});
   
Route::group(['middleware' => ['api']], function() {

   Route::get('/api/vi/songs', 'SongController@index');
   Route::get('/api/vi/song/{id}', 'SongController@fetchData');

});

By default, it applies a rate limit of 60 requests per minute to all the routes above.

You can apply other middleware to the api group by just adding it in the api array in Kernel.php.

It’s that simple.