php - Set Parameter Variable Globally at all the backend Yii2 -


i have code in layout/main.php:

    $userid = yii::$app->user->id;     $data = user::find()->where('id ="'.$userid.'"')->one();     $type = $data['type'];     yii::$app->view->params['type'] = $type; 

i cant' access $this->params['type'] controller except sitecontroller. if navigated using controller got error:

 php notice – yii\base\errorexception  undefined index: type 

if duplicated query in every controller, works well. don't want that. how make globally @ backend only?

you can accomplish (without explicitly session coding) by

in model (user.php)

    /**      * @inheritdoc      */     public static function findidentity($id)     {         // return isset(self::$users[$id]) ? new static(self::$users[$id]) : null;         $user = static::findone(['id'=>$id]);         if(!$user)         {             return null;         }         else         {             $dbuser =              [                 'id' => $user->id,                 'image'=>$user->image,                 'type'=>$user->type,                 // 'usertype'=>$user->user_type, set other attributes                 'username' => $user->username,                 'first_name' => $user->first_name,                 'last_name' => $user->last_name,                 'email' => $user->email,             ];             return new static($dbuser);         }     } 

now if user has logged in successfully, use attributes globally (through out project eg in model, view controller) by

for eg

echo yii::$app->user->identity->id; echo yii::$app->user->identity->image; echo yii::$app->user->identity->type; echo yii::$app->user->identity->username; echo yii::$app->user->identity->frst_name; echo yii::$app->user->identity->last_name; echo yii::$app->user->identity->email; 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -