如何在yii框架中访问用户详细信息?

时间:2013-07-05 15:57:37

标签: php drupal yii

我是Yii的初学者。我想在整个应用程序中访问user_id。我怎么能这样做?

class MainController extends Controller {
    public function actionInitialize() {                   
                             $this->verifyDrupalUserIdentity(); 

          }

 private function verifyDrupalUserIdentity() {
                    new DrupalUserIdentity();                   
          }

}


class DrupalUserIdentity extends CUserIdentity {
 public function __construct() {
                    $this->authenticate();
          }
 public function authenticate(){

                    global $base_url;
                    $base_url = BASE_URL.'CA_Hive';
                    $currentPath = getcwd();
                    require_once DRUPAL_ROOT.'/includes/bootstrap.inc';
                    require_once DRUPAL_ROOT.'/includes/errors.inc';
                    drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
                    chdir($currentPath);

                    global $user;
                    $this->username = $user->name;
                    new DrupalUser();
                    Yii::app()->user->setDrupalAttributes($user);
          }
}

**

  • 型号: -

**

    class DrupalUser extends CWebUser {
     private $_attributes;
              private $_browser;
    public $user_id;
     public function setDrupalAttributes($attributes) {
                        $this->_attributes = $attributes;
                        $this->user_id = $this->_attributes->uid;
              }
     public function getUserId() {
return $this->user_id;
}

    }

现在我可以使用 Yii :: app() - > user-> getUserId();

访问 MainController 中的用户ID

但问题是,我无法访问 SecondController 中的user_id。如果我尝试访问它们,它会给出null值。需要帮助。

SecondController

class SecondController extends Controller {


 public function getUserDetail() {
                   // Here i want the user ID                
          }

}

2 个答案:

答案 0 :(得分:0)

您应该向我们展示您的SecondController代码,您是否已将其从Controller课程延伸出来?顺便说一句,这是一回事,我不能问,Drupal在这里是什么?

答案 1 :(得分:0)

您不需要控制器中的任何代码。当前用户由Yii中的user组件表示。您可以通过id从任何地方访问Yii::app()->user->id

如果要存储该用户的自定义数据,可以扩展CUserIdentity。有关示例,请查看手册here