문제

에 Yii2 액세스할 수 있습니다 id 인터페이스에 대한 현재 사용하여 사용자에 identityInterface 개체 내에서\yii\웹\사용자는 클래스가 다음과 같이

 \Yii::$app->user->identity->id;

이 있을 얻을 수 있는 방법 설정 매개변수(없이 확장하는 id 등)?

기본적으로 동등한의 Yii1.x getState(), 고 setState() 방법 CWebUser 를 저장하고 검색 세션 정보는 다음과 같이

Yii::app()->user->setState("some_attribute",$value);
Yii::app()->user->getState('some_attribute',$defaultValue);

다른 팁

사용자에게 속성을 추가하는 가장 좋은 방법은 사용자 클래스에 공개 속성을 생성하는 것입니다.

class Yiidentity extends ActiveRecord implements \yii\web\IdentityInterface{
       public $awesomeAttribute = "foo";
}
.

사용 방법 :

Yii::$app->user->identity->awesomeAttribute;
.

yii2 \ yii :: $ app-> user-> Identity 는 IdentityClass의 인스턴스를 포함합니다.따라서 일부 IdentityClass 관련 데이터를 원한다면 IdentityClass의 인스턴스와 같은 것만으로 사용하십시오.

수도 있습 사용 getter 포함하는 기능을 추가로 신원 정보를 이용자에게 있습니다.예를 들어 검색 fullname 프로필에서 테이블.

class User extends ActiveRecord implements \yii\web\IdentityInterface
{
    // Other code goes here...       
    public function getFullname()
    {
        $profile = Profile::find()->where(['user_id'=>$this->id])->one();
        if ($profile !==null)
            return $profile->fullname;
        return false;
    }
}

다음을 사용할 수 있습으로 그것이 있는 경우 라는 특성 fullname, 다음과 같이

Yii::$app->user->identity->fullname;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top