基于多个用户角色的路由级中间件
Middleware on route level based on multiple user roles
我在 laravel 应用程序中有大约 10 种类型的用户角色,例如
user_type_1 and user_type 4 are accessing url-1.
user_type_7, user_type_5 and user_type 4 are accessing url-2.
user_type_5, user_type_1, user_type_3 and user_type 6 are accessing url-3.
............................................
............................................
n number of combination of routes. url according to user type.
我的 route/web.php
文件有大约 1500 条路由,目前没有使用中间件分组。我必须限制用户只能使用那种 user_type 被授权的 urls。能否请您提出任何更好的方法来做到这一点。
我曾尝试将 url 与中间件组结合使用,如下所示,但经过几次工作后就放弃了这种方法。
Route::group(['middleware' => ['middleware_user_type_1', 'middleware_user_type_2']], function () {
Route::get('url-1', 'XYZController@someMethod');
});
以这种方式,请求首先转到数组中的第一个中间件,如果不是有效的用户类型,则不尝试使用第二个中间件。
您需要实现一个中间件并将用户类型传递给它。
Route::group(['middleware' => ['check_user_type:type_1,type_2']], function () {
Route::get('url-1', 'XYZController@someMethod');
});
看看spatie/laravel-permission角色中间件是如何实现类似逻辑的。
Route::group(['middleware' => ['role:super-admin|writer']], function () {
//
});
中间件然后通过分隔符分解角色字符串,然后检查当前用户是否有任何角色。
您可以在路由组中定义中间件和角色。首先你要在中间件中定义角色管理。
Route::group(['middleware' => ['roles'], 'roles' => ['role_1','role_2], function () {
Route::get('/index', [
'uses' => 'ExampleController@index',
'as'=> 'index'
]);
});
开箱即用,您可以实施政策:
https://laravel.com/docs/5.6/authorization#creating-policies
此致。
我在 laravel 应用程序中有大约 10 种类型的用户角色,例如
user_type_1 and user_type 4 are accessing url-1.
user_type_7, user_type_5 and user_type 4 are accessing url-2.
user_type_5, user_type_1, user_type_3 and user_type 6 are accessing url-3.
............................................
............................................
n number of combination of routes. url according to user type.
我的 route/web.php
文件有大约 1500 条路由,目前没有使用中间件分组。我必须限制用户只能使用那种 user_type 被授权的 urls。能否请您提出任何更好的方法来做到这一点。
我曾尝试将 url 与中间件组结合使用,如下所示,但经过几次工作后就放弃了这种方法。
Route::group(['middleware' => ['middleware_user_type_1', 'middleware_user_type_2']], function () {
Route::get('url-1', 'XYZController@someMethod');
});
以这种方式,请求首先转到数组中的第一个中间件,如果不是有效的用户类型,则不尝试使用第二个中间件。
您需要实现一个中间件并将用户类型传递给它。
Route::group(['middleware' => ['check_user_type:type_1,type_2']], function () {
Route::get('url-1', 'XYZController@someMethod');
});
看看spatie/laravel-permission角色中间件是如何实现类似逻辑的。
Route::group(['middleware' => ['role:super-admin|writer']], function () {
//
});
中间件然后通过分隔符分解角色字符串,然后检查当前用户是否有任何角色。
您可以在路由组中定义中间件和角色。首先你要在中间件中定义角色管理。
Route::group(['middleware' => ['roles'], 'roles' => ['role_1','role_2], function () {
Route::get('/index', [
'uses' => 'ExampleController@index',
'as'=> 'index'
]);
});
开箱即用,您可以实施政策:
https://laravel.com/docs/5.6/authorization#creating-policies
此致。