Home >Backend Development >PHP Tutorial >Introduction to the usage of user in yii2 (with code)

Introduction to the usage of user in yii2 (with code)

不言
不言Original
2018-07-24 09:36:462848browse

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.

yii2-user Usage

user models

<?php

namespace app\models;

use dektrium\user\models\User as BaseUser;

use Yii;
class User extends BaseUser  // 这记得要继承
{

}

Configuration web.php components

    &#39;user&#39; => [
        'identityClass' => 'app\models\User',
        'loginUrl' => ['site/login'],
        'enableAutoLogin' => true,
    ],

Configuration web.php modules

    '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',
            ],
        ],
    ],

Use

Yii::$app->user->login($user, $duration);
At this time, you can

You can directly obtain the user’s information globally

Get the id

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

Get the user name

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn