Which is great it shows the sign in link when the user is a guest and when the user is logged in it shows the logout(Users First Name) link. But when I actually log out I get this error "CWebUser.firstName" presumably because this code
Logout ('.Yii::app()->user->firstName.')
results in an unknown variable as I assign the firstName from the DB to the session at login.
What’s the Yii way to stop that line failing?? (Having a good time with Yii)
As I said I believe that to be because once you are logged in I assign your firstName from the database to the Yii::app()->user. But if you are not logged in or then log out Yii:app()->user->firstName doesn’t actually exist. So I just wanted to know how I can overcome this problem the Yii way. As I’m new to Yii.
Here is how I assign the data at login
public function authenticate()
{
$user = Users::model()->findByAttributes(array('email'=>$this->username));
if ($user===null) { // No user found!
$this->errorCode=self::ERROR_USERNAME_INVALID;
} else if ($user->pass !== SHA1($this->password) ) { // Invalid password!
$this->errorCode=self::ERROR_PASSWORD_INVALID;
} else { // Okay!
$this->errorCode=self::ERROR_NONE;
// Store the role in a session:
$this->setState('type', $user->type); // store user type
$this->_id = $user->id; // store user id
$this->setState('firstName', $user->first_name); // store user's first name
}
return !$this->errorCode;
}
this is because the CWebUser does not have the property named firstName so this is the problem. if you want to access the current user’s name you can use the name property instead of firstName for more details read here