Yii::app()->;用户->;身份证件获取用户名而不是ID
我正在尝试获取用户id,但到目前为止没有运气Yii::app()->;用户->;身份证件获取用户名而不是ID,yii,Yii,我正在尝试获取用户id,但到目前为止没有运气 echo Yii::app()->user->id 及 echo Yii::app()->user->getId() 返回奇怪的用户名。 知道怎么回事吗?用户标识中应该有getId函数Yii::app()->默认情况下,用户返回组件CWebUser 当您想要获得有关用户的其他信息时,需要扩展此组件 在components文件夹中创建一个文件WebUser.php。(我下面的例子) 在配置文件中查找部分 'components'=>array(
echo Yii::app()->user->id代码>
及
echo Yii::app()->user->getId()代码>
返回奇怪的用户名。
知道怎么回事吗?用户标识中应该有getId函数Yii::app()->默认情况下,用户返回组件CWebUser
当您想要获得有关用户的其他信息时,需要扩展此组件
在components
文件夹中创建一个文件WebUser.php
。(我下面的例子)
在配置文件中查找部分
'components'=>array(
'user'=>array(
'class'=>'WebUser'
)
)
如果没有这个部分,就创建它。并将'class'=>更改为WebUser'
在组件/用户标识中使用getid函数获取用户标识和名称等
或者您可以使用设置状态
:
class UserIdentity extends CUserIdentity
{
private $_id;
public function authenticate()
{
$record=Users::model()->findByAttributes(array('mail'=>$this->username));
if($record===null)
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if($record->password!==md5($this->password."325"))
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
{
$this->_id = $record->id;
$this->setState('role', $record->active);
$this->setState('mail', $record->mail);
$this->setState('name', $record->name);
$this->setState('surname', $record->surname);
$this->setState('mobile', $record->mobile);
$this->setState('adress', $record->adress);
$record->count_login += 1;
$record->update();
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}
public function getId()
{
return $this->_id;
}
}
<?php echo Yii::app()->user->name." ".Yii::app()->user->surname; ?>
类用户标识扩展了用户身份
{
私人$u id;
公共函数身份验证()
{
$record=Users::model()->findByAttributes(数组('mail'=>$this->username));
如果($record==null)
$this->errorCode=self::ERROR\u USERNAME\u无效;
否则如果($record->password!==md5($this->password.325”))
$this->errorCode=self::ERROR\u PASSWORD\u无效;
其他的
{
$this->\u id=$record->id;
$this->setState('role',$record->active);
$this->setState('mail',$record->mail);
$this->setState('name',$record->name);
$this->setState('姓氏',$record->姓氏);
$this->setState('mobile',$record->mobile);
$this->setState('address',$record->address);
$record->count\u login+=1;
$record->update();
$this->errorCode=self::ERROR\u NONE;
}
return!$this->errorCode;
}
公共函数getId()
{
返回$this->\u id;
}
}
如果要在视图上显示它,应执行以下操作:
echo Yii::$app->user->id;
为什么我们使用“组件”=>array('user'=>array('class'=>'WebUser'))
?用于添加额外类?@Riaz用于扩展CWebUserbehaviors@RiazKhan:要告诉Yii它应该创建您自己的WebUser
类(提供此额外功能)的实例,而不是内置的CWebUser
。如上所述,您需要在标识类中实现getId()。其解释如下:
class UserIdentity extends CUserIdentity
{
private $_id;
public function authenticate()
{
$record=Users::model()->findByAttributes(array('mail'=>$this->username));
if($record===null)
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if($record->password!==md5($this->password."325"))
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
{
$this->_id = $record->id;
$this->setState('role', $record->active);
$this->setState('mail', $record->mail);
$this->setState('name', $record->name);
$this->setState('surname', $record->surname);
$this->setState('mobile', $record->mobile);
$this->setState('adress', $record->adress);
$record->count_login += 1;
$record->update();
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}
public function getId()
{
return $this->_id;
}
}
<?php echo Yii::app()->user->name." ".Yii::app()->user->surname; ?>
echo Yii::$app->user->id;