YII2.0高级版使用MySql数据库完善用户注册登录功能

安装完YII2.0高级版之后,需要配置数据库连接,我使用的是MySql数据库,配置文件main-local.php位于安装目录的common文件夹中,例如我的就在文件夹D:\WampServer\wamp\www\common\config中。

这里需要配置网站使用的数据库名称,以及数据库的登录名与密码

<?php
return [
    'components' => [
        'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'mysql:host=localhost;dbname=yii2advanced',
            'username' => 'root',
            'password' => 'root',
            'charset' => 'utf8',
        ],
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            'viewPath' => '@common/mail',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
            'useFileTransport' => true,
        ],
    ],
];

配置中db段就是数据库配置。

配置完数据库之后,需要按照配置创建数据库yii2advanced表,排序规则我使用的是utf8_unicode_ci

然后使用cmd命令行工具运行yii migrate命令创建user表,注意需要先cd到网站目录


输入yes,回车


然后数据库中就多了user表,此时用户注册与登录退出功能就可以使用了。

猜你喜欢

转载自blog.csdn.net/waterlily_5/article/details/80330618
今日推荐