The content shared with you in this article is about how to use yii2-user. The content is of great reference value. I hope it can help friends in need.
<?php namespace app\models; use dektrium\user\models\User as BaseUser; use Yii; class User extends BaseUser // 这记得要继承 { }
'user' => [ 'identityClass' => 'app\models\User', 'loginUrl' => ['site/login'], 'enableAutoLogin' => true, ],
'modules' => [ 'v1' => [ 'class' => 'app\modules\v1\Module', ], 'admin' => [ 'class' => 'mdm\admin\Module', 'layout' => 'left-menu',//yii2-admin的导航菜单 ], // 'rbac' => 'dektrium\rbac\RbacWebModule', 'rbac' => [ 'class' => 'dektrium\rbac\Module', ], 'user' => [ 'class' => 'dektrium\user\Module', 'enableRegistration' => false, 'enableConfirmation' => false, 'enableUnconfirmedLogin' => true, 'enablePasswordRecovery' => true, 'confirmWithin' => 21600, 'rememberFor' => 1209600, //如果没有点击记住密码则默认保持1天的登录时间 'admins' => ['admin'], 'modelMap' => [ 'User' => 'app\models\User', // 'Profile' => 'app\models\Profile', ], ], ],
Yii::$app->user->login($user, $duration);
At this time, you can
Yii::$app->user->identity->id
Yii::$app->user->identity->name
Related recommendations:
In-depth analysis of Laravel pipeline (code)
How PHP custom recursive function implements the function of converting arrays to JSON
The above is the detailed content of Introduction to the usage of user in yii2 (with code). For more information, please follow other related articles on the PHP Chinese website!