php Laravel 中间件除了 Route::group

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28566071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:56:27  来源:igfitidea点击:

Laravel Middleware except with Route::group

phplaravel-5laravel-routinglaravel-middleware

提问by Sebastian Sulinski

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?

我将如何使用上述设置为中间件声明异常?

回答by lukasgeiter

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

        // ...
    });
});