I would like to have the $ user_profile variable available in most of my view files, without having to create a variable in every controller file. Everything works for me at the moment, but I was wondering if there is a better solution
I have code to populate a variable
$user_profile = YumUser::model()->findByPk(Yii::app()->user->id)->profile;
Then parent class
class Controller extends CController { public function getUserProfile() { $user_profile = YumUser::model()->findByPk(Yii::app()->user->id)->profile; } }
Then I have all the other controllers inheriting the Controller class, for example
class DashboardController extends Controller { public function actionIndex() { $user_profile = parent::getUserProfile(); $this->render('index', array('user_profile' => $user_profile)); } }
Then, finally, in the view file, I can just access the $ user_profile variable.
source share