Patricia Beier Patricia Beier - 4 months ago 16
PHP Question

Get the name of the user in yii2

How can I get the name of the logged-in-user in yii2?
I can get the user-id with

Yii::$app->user->id;


and I know that I could find the name in the database but I want a direct way.
The name-column in the database has the name "username", but

Yii::$app->user->username;


doesn't work and

Yii::$app->user->name;


doesn't work either.

Answer

On login the user information will be stored in Yii::$app->user->identity variable.

For more information have a read through the User Authentication documentation in the official guide.

Comments