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

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! 

 

posted @ 2012-09-07 14:01  DavidHHuan  阅读(436)  评论(0编辑  收藏  举报