Yii2获取登录用户的角色
有人能给我解释一下如何使用这个功能吗Yii2获取登录用户的角色,yii2,Yii2,有人能给我解释一下如何使用这个功能吗 getRoleByUser() 用于获取登录用户角色的相关表 我试过这个 $Role = Yii::$app->authManager->getRolesByUser(Yii::$app->user->getId()); print_r($Role); exit(); 但是我犯了以下错误 Getting unknown property: yii\web\Application::findByUsername 试
getRoleByUser()
用于获取登录用户角色的相关表
我试过这个
$Role = Yii::$app->authManager->getRolesByUser(Yii::$app->user->getId());
print_r($Role);
exit();
但是我犯了以下错误
Getting unknown property: yii\web\Application::findByUsername
试试这个
\Yii::$app->authManager->getRolesByUser($user_id);
从身份验证管理器中可以获得它。使用
$userRole = Yii::$app->authManager->getRolesByUser(Yii::$app->user->getId());
像
错误似乎与您发布的代码无关-您的
WebUser
组件中可能有一些错误。
if ($userRole) {
foreach ($userRole as $role) {
$roles[] = $role->name;
}
// if user have 1 role then $userRole will be a string containing it
// othewhise let $userRole be an array containing them all
$userRole = count($roles) === 1 ? $roles[0] : $roles ;
}