如何使用Zend framework2获取用户角色


How do I get the user role using Zend framework2?

我使用的是Zend Framework 2,在获取用户角色时遇到问题。我使用的是zfc user和zfc Rbac。角色目前可以工作,但我希望获得在if语句(在控制器中)中使用的值。调用这些函数的函数是什么?我打开了开发人员工具,这样我就可以看到分配的角色,但不知道该怎么称呼它

谢谢Matt

如果您已经在ZfcRbac中正确识别了设置,那么您可以使用授权服务(ZfcRbat''service''AuthorizationService)来获取身份及其角色。

$authorizationService->getIdentity()->getRoles();

我使用了下面的代码,并且能够获得登录用户的角色。

 $viewmodel = new ViewModel();
 $authorize = $this->getServiceLocator()->get('UserRbac'Identity'IdentityRoleProvider');
 $roles = $authorize->getIdentityRoles();
 echo $roles[0];
    $viewmodel->setVariable("roles", $roles);
return $viewmodel;