Yii2.0 Framework

How to Get Logged In User Details in Yii2

In this tutorial, we will show how you can get the logged in user details in Yii2. After the successful login the user information will be stored in Yii::$app->user->identity variable.

The authenticated user identity information can be retrieved via Yii::$app->user->identity.

Get Logged In User Details:

The below php script will return the logged in user details:

<?php 
//To get whole logged user data
$user = \Yii::$app->user->identity;
	
//To get id of logged user
$userId = \Yii::$app->user->identity->id;
	
//To get username of the logged in user
\Yii::$app->user->identity->username;
?>

References:

http://www.yiiframework.com/doc-2.0/yii-web-user.html
http://www.yiiframework.com/doc-2.0/guide-index.html#getting-data-from-users

1

No Responses

Write a response