2015-05-20 78 views
0

我有一个相当复杂的应用程序,它使用自定义身份验证中间件。我有一个路由组是这样的:Laravel嵌套路由组逆中间件

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

    Route::get('/', function() { 

     echo 'private'; 
    }); 

    Route::group(['prefix' => 'public'], function() { 

     Route::get('/', function() { 

      echo 'public'; 
     }); 
    }) 
}); 

现在auth中间件将重定向未经过身份验证的所有请求。带有auth前缀的主群组已通过身份验证。但是,即使用户未通过身份验证,我也希望组public可以访问。

所以:

http://example.com/auth < must be authenticated 
http://example.com/auth/some-sub-page < must be authenticated 
http://example.com/auth/public < no need for authentication 

那么,有没有办法像'remove-middleware' => 'auth'添加到嵌套组与public前缀?或者我会不得不重组我的路线组?

回答

0

为什么不只是将auth中间件包装在非公共路线周围?

Route::group(['prefix' => 'auth'], function() { 

    // routes requiring auth 
    Route::group(['middleware' => 'auth']) { 

     Route::get('/', function() { 

      echo 'private'; 
     }); 

    } 

    // other routes 
    Route::group(['prefix' => 'public'], function() { 

     Route::get('/', function() { 

      echo 'public'; 
     }); 

    }); 
}); 

可能有办法做'删除中间件'类型的中间件,但这可能会变得混乱。这似乎是最干净的解决方案。