Laravel Middleware except with Route::group

Sebastian Sulinski picture Sebastian Sulinski · Feb 17, 2015 · Viewed 24.4k times · Source

I'm trying to create a group Route for the admin section and apply the middleware to all paths except for login and logout.

What I have so far is:

Route::group(['prefix' => 'admin', 'namespace' => 'Admin', 'middleware' => 'authAdmin'], function() {

    Route::resource('page', 'PageController');
    Route::resource('article', 'ArticleController');
    Route::resource('gallery', 'GalleryController');
    Route::resource('user', 'UserController');

    // ...

});

How would I declare exceptions for the middleware with the above setup?

Answer

lukasgeiter picture lukasgeiter · Feb 17, 2015

Simply nest groups and then you can exclude specific routes:

Route::group(['prefix' => 'admin', 'namespace' => 'Admin'], function() {

    Route::get('login', 'AuthController@login');
    Route::get('logout', 'AuthController@logout');

    Route::group(['middleware' => 'authAdmin'], function(){
        Route::resource('page', 'PageController');
        Route::resource('article', 'ArticleController');
        Route::resource('gallery', 'GalleryController');
        Route::resource('user', 'UserController');

        // ...
    });
});