您的位置:首页 > 编程语言 > PHP开发

[Yii Framework] how to add additional data to the user session

2012-09-07 14:01 661 查看
From the code generated by the yiic, as we know, using these code can only get id and username from Yii::app()->user after logined:

$identity = new UserIdentity($_POST['LoginForm']['username'], $_POST['LoginForm']['password']);
Yii::app()->user->login($identity);

but how about adding some additional data to the user session? Here is the example.

1. implement the method "getPersistentStates()" in UserIdentify, for example:

/*
*
*/
public function getPersistentStates()
{
return array(
'roleId' => $this->_user->role_id,
);
}then, you can get the data via Yii::app()->user->roleId after logined.

2. add the data wherever you want, just use these code:

Yii::app()->user->setState('mykey', 'myvalue');then, you can get the data via Yii::app()->user->mykey.

Have fun with Yii!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: