duanan6043 2018-03-29 09:55
浏览 72
已采纳

基于多个用户角色的路由级别的中间件

I have about 10 type of users role in laravel application, where e.g.

  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.

My route/web.php file have about 1500 routes and currently not seperated as group using middleware. I have to restrict user for only urls that is authorized for that kind of user_type. Can you please suggest any better approach for doing this.

I had tried with making combination of url with middleware group like below, but after few work left this approach.

Route::group(['middleware' => ['middleware_user_type_1', 'middleware_user_type_2']], function () {
        Route::get('url-1', 'XYZController@someMethod');
    });

In this way request first goes to first middleware in array and if not valid user type then not try with second middleware.

  • 写回答

3条回答 默认 最新

  • dongsimu4422 2018-03-29 10:18
    关注

    You need to implement one middleware and pass user types to it.

    Route::group(['middleware' => ['check_user_type:type_1,type_2']], function () {
            Route::get('url-1', 'XYZController@someMethod');
        });
    

    Take a look how similar logic implemented in spatie/laravel-permission role middleware.

    Route::group(['middleware' => ['role:super-admin|writer']], function () {
        //
    });
    

    Middleware then explodes roles string by the separator, and then check if the current user has any of the roles.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了