Danmo 2016-09-06 12:44:14 11339次浏览 7条评论 19 3 0

yii2-user是一个专注于用户登录注册的扩展,有效使用该扩展能大大减少实现登陆注册功能的时间。
yii2-user官网yii2-user扩展
本实现采用了YII2.0.9高级模板,下载安装如下:

php composer.phar create-project yiisoft/yii2-app-advanced advanced 2.0.9

下载完成后
在网站根目录下执行init 初始化命令

cd advanced
init

然后选择0 Development

Which environment do you want the application to be initialized in?

  [0] Development
  [1] Production

  Your choice [0-1, or "q" to quit] 0

  Initialize the application under 'Development' environment? [yes|no] yes

这样高级模板就安装好了。
安装好之后就是配置了,打开advanced/common/config/main-local.php

<?php
return [
    'components' => [
        'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'mysql:host=localhost;dbname=yii2_wechat',
            'username' => 'root',
            'password' => 'root',
            'charset' => 'utf8',
            'tablePrefix' => 'wx_',
        ],
        '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' => false, 
            'transport' => [ 
                'class' => 'Swift_SmtpTransport', 
                'host' => 'smtp.163.com', 
                'username' => '***@163.com', 
                'password' => '******', 
                'port' => '25', 
                'encryption' => 'tls', 
            ], 
        ],
    ],
];

上面有个坑,就是在advanced/common/config/params.php中的

<?php
return [
    'adminEmail' => '***@163.com',
    'supportEmail' => '***@163.com',
    'user.passwordResetTokenExpire' => 3600,
];

还有advanced/frontend/config/params.php也要修改

<?php
return [
    'adminEmail' => '***@163.com',
];

接下来我们安装YII2-user扩展
同样在网站根目录下,也就是在advanced里执行以下代码

composer require "dektrium/yii2-user:0.9.*@dev"

因为YII2-USER我们只打算用在前台frontend,所以在advanced/frontend/config/main.php中添加以下代码,modules和components是同级关系。

'modules' => [
    'user' => [
        'class' => 'dektrium\user\Module',
    ],
],

然后返回advanced根目录下,也就是advanced里面,执行数据迁移

yii migrate/up --migrationPath=@vendor/dektrium/yii2-user/migrations

然后我们打开advanced/frontend/views/layouts/main.php文件,是为了使得登陆注册使用yii2-user而不是用YII2默认的登陆注册,修改为以下代码

    <?php
    NavBar::begin([
        'brandLabel' => 'My Company',
        'brandUrl' => Yii::$app->homeUrl,
        'options' => [
            'class' => 'navbar-inverse navbar-fixed-top',
        ],
    ]);
    $menuItems = [
        ['label' => 'Home', 'url' => ['/site/index']],
        ['label' => 'About', 'url' => ['/site/about']],
        ['label' => 'Contact', 'url' => ['/site/contact']],
        ['label' => 'Wechat', 'url' => ['/wechat/default']],
    ];
    if (Yii::$app->user->isGuest) {
        $menuItems[] = ['label' => 'Signup', 'url' => ['/user/registration/register']];
        $menuItems[] = ['label' => 'Login', 'url' => ['/user/security/login']];
    } else {
        $menuItems[] = '<li>'
            . Html::beginForm(['/user/security/logout'], 'post')
            . Html::submitButton(
                'Logout (' . Yii::$app->user->identity->username . ')',
                ['class' => 'btn btn-link']
            )
            . Html::endForm()
            . '</li>';
    }
    echo Nav::widget([
        'options' => ['class' => 'navbar-nav navbar-right'],
        'items' => $menuItems,
    ]);
    NavBar::end();
    ?>

然后在浏览器中输入localhost/advanced/frontend/web/index.php,点击signup就可以注册并发邮件激活自己的账号了。

觉得很赞
  • 评论于 2016-09-09 11:06 举报

    数据迁移的目的是啥,迁移之前的数据库不能东西么,

  • 评论于 2016-09-09 11:35 举报

    数据迁移的时候出现的错误

    Exception 'yii\db\Exception' with message 'could not find driver'
    
    in D:\wamp\www\yii\vendor\yiisoft\yii2\db\Connection.php:550
    

    网上的处理方法我用了,但是不行,我已经吧localhost改为了127.0.0.1

    好吧,已经处理了,是因为pdo_mysql扩展没打开的原因。

  • 评论于 2016-09-20 09:10 举报

    这个组件很强大,希望能够更详细的介绍下 例如怎么在后台CRUD

  • 评论于 2016-10-26 10:32 举报

    如果出现无法注册或收不到验证邮件只需把

     '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' => false, 
                'transport' => [ 
                    'class' => 'Swift_SmtpTransport', 
                    'host' => 'smtp.163.com', 
                    'username' => '***@163.com', 
                    'password' => '******', 
                    'port' => '25', 
                    'encryption' => 'tls', 
                ], 
            ],
    

    改为

     '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' => false, 
                'transport' => [ 
                    'class' => 'Swift_SmtpTransport', 
                    'host' => 'smtp.163.com', 
                    'username' => '***@163.com', 
                    'password' => '******', 
                    'port' => '25', 
                    'encryption' => '', 
                ], 
            ],
    
    觉得很赞
  • 评论于 2016-11-17 17:27 举报

    还用的插件。

  • 评论于 2016-12-08 14:54 举报

    安装运行后,邮件发送成功但是登陆报错: Column not found: 1054 Unknown column 'status' in 'where clause'。
    说user表中没有status字段,我看了下user表,的确没有这个字段,应该怎么解决?自己加一个status?

  • 评论于 2016-12-26 17:12 举报

    这个user组件如何实现单点登录

您需要登录后才可以评论。登录 | 立即注册