duankuiyu4618 2019-03-11 16:14
浏览 183
已采纳

将多个中间件添加到Laravel项目

I'm new to laravel I have created middleware for my each role but when I add it to my route it won't work.

If I add single middleware to my route it works fine but when I add second and third one It will not work.

It won't shows the route to authorized user it redirect it to home,

My User Model

public function IsAdmin()
{
    if($this->role_id =='1')
    {
        return true;
    }
    else
    {
        return false;
    }
}

public function IsManager()
{
    if($this->role_id =='2')
    {
        return true;
    }
    else
    {
        return false;
    }
}

public function IsUser()
{
    if($this->role_id =='3')
    {
        return true;
    }
    else
    {
        return false;
    }
}

My Kernal

'IsAdmin' => \App\Http\Middleware\IsAdmin::class,
'IsManager' => \App\Http\Middleware\IsManager::class,
'IsUser' => \App\Http\Middleware\IsUser::class,

My IsAdmin Middlewares

public function handle($request, Closure $next)
{
    $user =Auth::User();
    if(!$user->IsAdmin())
    {
        return redirect('stock');
    }
    return $next($request);
}

My IsManager

public function handle($request, Closure $next)
{
    $user =Auth::User();
    if(!$user->IsManager())
    {
        return redirect('stock');
    }
    return $next($request);
}

and IsUser

public function handle($request, Closure $next)
{
    $user =Auth::User();
    if(!$user->IsUser())
    {
        return redirect('stock');
    }
    return $next($request);
}

and finally Route

Route::get('approv',['middleware'=>['IsManager','IsAdmin'],function(){
    return view('approv');
}]);
  • 写回答

1条回答 默认 最新

  • dpowhyh70416 2019-03-11 16:39
    关注

    This will not work as you'd expect. All middleware need to pass in order for the request to be processed which means that your user will need to be both a manager and an admin at the same time which based on your setup is impossible.

    You can get around this (kind of) by making a different kind of middleware:

    Kernel:

    'roles' => \App\Http\Middleware\Roles::class,
    

    And the Roles middleware:

    class Roles {
    
        private function checkRole($role) {
              switch ($role) {
                  case 'user': return \Auth::user()->IsUser();
                  case 'manager': return \Auth::user()->IsManager();
                  case 'admin': return \Auth::user()->IsAdmin();
              }
              return false;
        }
    
        public function handle($request, Closure $next, ...$roles) 
        {
             foreach ($roles as $role) {
                 if ($this->checkRole($role)) {
                     //At least one role passes
                     return $next($request);
                 }
             } 
             //All checks failed so user does not have any of the required roles
             return redirect('stock');  
        }
    }
    

    Then to use this you simply do:

    Route::get('approv',['middleware'=>['roles:manager,admin'],function(){
       return view('approv');
    }]);
    

    This works because Laravel Middleware support parameters. You can pass parameters as a comma separated list of strings where you declare the middleware. In this case this was done as roles:manager,admin

    Laravel will then send these parameters as additional parameters in the handle method. These can be accessed using PHPs syntax for variadic arguments. In this particular case it's by using the array spread operator. This is documented as an example in the function arguments section of the PHP manual.

    Note that this is actually equivalent to saying :

      public function handle($request, Closure $next, $role1=null, $role2=null, $role3=null)
    

    but using the spread operator is much more convenient since ...$roles would be an array which contains only the roles that were passed in the middleware.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?