简体   繁体   中英

Laravel Middleware except with Route::group

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?

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

        // ...
    });
});

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]);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM