doushai7225 2018-07-03 10:06
浏览 74
已采纳

爆炸功能在laravel中间件中不起作用

I'm developing a Laravel ACL System. I have a User table, a role table and a permissions table. When I check the user given permissions using middleware and inside middleware I'm use explode() function. this function only show the first permissions other permissions doesn't contains.

Here, is my Controller;

function __construct()
{
    $this->middleware('auth');
    $this->middleware('HasPermission:Role-Read,Role-Update,Role-Delete');
}

My Middleware.

 public function handle($request, Closure $next,$permissions='')
{
    $permissions_array = explode(',', $permissions);
    dd($permissions_array);
    foreach($permissions_array as $permission){
        if(Auth::user()->hasPermission($permission)){
            return $next($request);
        }
    }

    return redirect()->back();;
}

result when i use dd() function.

array:1 [▼ 0 => "Role-Read" ]

  • 写回答

1条回答 默认 最新

  • dongshao8125 2018-07-03 10:17
    关注

    Controller

    $this->middleware('HasPermission:Role-Read,Role-Update,Role-Delete');
    

    Middleware Using ... to access variable arguments

    public function handle($request, Closure $next, ...$permissions)
        {
            foreach($permissions_array as $permission){
                if(Auth::user()->hasPermission($permission)){
                    return $next($request);
                }
            }
    
            return redirect()->back();;
        }
    

    or you can use

    $this->middleware('HasPermission:Role-Read|Role-Update|Role-Delete');
    

    Middleware

    $permissions_array = explode('|', $permissions);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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