M
M
m4tkv2019-10-23 23:34:15
Yii
m4tkv, 2019-10-23 23:34:15

How to get user->identity variable in view?

Please tell me how to access user->identity variable in view.
It is clear that I can do this in the view: I can declare the $user variable at the beginning of the view and spin around it I can throw $user into the view right in the controller:
<span><?=Yii::$app->user->identity->name?></span>
$user = Yii::$app->user->identity;

$user = Yii:$app->user->identity;
return $this->render('master',compact('user'));

But I'm sure there is a way to use $user->identity directly in the view, but I don't know how. If this is not possible, please tell me how.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2019-10-23
@m4tkv

I'm afraid to upset you, but there is no other way)) For this, there is a special Identity to work with an authorized user.
The only thing I would separate authentication and users. Then you can add your method there and implement this:

public function getName(): string
    {
       //Здесь ваша проверка
        return $this->user ? $this->user->getName() : 'БЕЗ ИМЕНИ!'; 
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question