我是 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 。如果我尝试访问它们,它会给出空值。需要帮忙。
第二控制器
class SecondController extends Controller {
public function getUserDetail() {
// Here i want the user ID
}
}