29

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?

Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
Sebastian Sulinski
  • 5,815
  • 7
  • 39
  • 61

2 Answers2

57

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');

        // ...
    });
});
lukasgeiter
  • 147,337
  • 26
  • 332
  • 270
4

You can also use laravel's withoutMiddleware method as below;

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');

  Route::get('login', 'AuthController@login')->withoutMiddleware([AuthAdminMiddleware::class]);
  Route::get('logout', 'AuthController@logout')->withoutMiddleware([AuthAdminMiddleware::class]);
});
Tuncay Elvanagac
  • 1,048
  • 11
  • 13