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条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?