是的,可以使用布局和基本控制器。
如果你来自 Yii 代码生成器,components 文件夹中应该有一个 Controller 类。
如果你的控制器是ExampleController extends Controller 而不是CController,
在Controller你可以分配:
public function getUserProfile() {
return YumUser::model()->findByPk(Yii::app()->user->id)->profile;
}
在你的布局文件中:
<?php echo CHtml::encode($this->getUserProfile()); ?>
因为$this指的是控制器,而控制器继承了名为$user_profile的属性。
但是,您应该在登录会话时分配profile 和其他不会与setState 不同的内容。这样您就可以执行以下操作:
<p class="nav navbar-text">Welcome, <i><?php echo Yii::app()->User->name; ?></i></p>
在 MySQLUserIdentity 中设置状态的示例(由我完成)。
class MySqlUserIdentity extends CUserIdentity
{
private $_id;
public function authenticate()
{
$user = User::model()->findByAttributes( array( 'username' => $this->username ) );
if( $user === null )
$this->errorCode = self::ERROR_USERNAME_INVALID;
else if( $user->password !== md5( $this->password ) )
$this->errorCode = self::ERROR_PASSWORD_INVALID;
else
{
$this->_id = $user->id;
$this->setState( 'username', $user->username );
$this->setState( 'name', $user->name );
$this->setState( 'surname', $user->surname );
$this->setState( 'email', $user->email );
$this->errorCode = self::ERROR_NONE;
}
return !$this->errorCode;
}
public function getId()
{
return $this->_id;
}
}