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

不言
Release: 2023-04-03 11:20:02
Original
2736 people have browsed it

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  // 这记得要继承
{

}
Copy after login

Configuration web.php components

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

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',
            ],
        ],
    ],
Copy after login

Use

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

You can directly obtain the user’s information globally

Get the id

Yii::$app->user->identity->id
Copy after login

Get the user name

Yii::$app->user->identity->name
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!