dektrium/yii2-user
的代碼在github
上的託管地址:https://github.com/dektrium/yii2-user,
使用composer
方式引入類php
composer require "dektrium/yii2-user:0.9.*@dev"
main.php
的組件請確保你沒有在你的配置文件中使用
user
組件配置前端
配置以下:git
'modules' => [ 'user' => [ 'class' => 'dektrium\user\Module', ], ],
$ php yii migrate/up --migrationPath=@vendor/dektrium/yii2-user/migrations
跳轉地址使用了url美化,請確保配置了
urlManager
組件github
@app\views\layouts\main.php
文件中將後端
if (Yii::$app->user->isGuest) { $menuItems[] = ['label' => 'Signup', 'url' => ['/site/signup']]; $menuItems[] = ['label' => 'Login', 'url' => ['/site/login']]; } else { $menuItems[] = '<li>' . Html::beginForm(['/site/logout'], 'post') . Html::submitButton( 'Logout (' . Yii::$app->user->identity->username . ')', ['class' => 'btn btn-link'] ) . Html::endForm() . '</li>'; }
替換爲bash
if (Yii::$app->user->isGuest) { $menuItems[] = ['label' => 'Sign in', 'url' => ['/user/security/login']]; $menuItems[] = ['label' => 'Register', 'url' => ['/user/registration/register'], 'visible' => Yii::$app->user->isGuest]; } else { $menuItems[] = ['label' => 'Sign out (' . Yii::$app->user->identity->username . ')', 'url' => ['/user/security/logout'], 'linkOptions' => ['data-method' => 'post']]; }
當你註冊新用戶後,該擴展默認會發送郵件,必須郵箱驗證後才能正式登錄,若是須要修改配置參數請查閱官方文檔yii2
session
不能用來登錄後端在@frontend\config\main.php
中配置項以下cookie
'components' => [ 'user' => [ 'identityCookie' => [ 'name' => '_frontendIdentity', 'path' => '/', 'httpOnly' => true, ], ], 'session' => [ 'name' => 'FRONTENDSESSID', 'cookieParams' => [ 'httpOnly' => true, 'path' => '/', ], ], ],
在@backend\config\main.php
中配置項以下session
'components' => [ 'user' => [ 'identityCookie' => [ 'name' => '_backendIdentity', 'path' => '/admin', 'httpOnly' => true, ], ], 'session' => [ 'name' => 'BACKENDSESSID', 'cookieParams' => [ 'httpOnly' => true, 'path' => '/admin', ], ], ],
1.用戶登錄後,點擊註銷登錄,錯誤提示爲After logging in I'm redirected back without any sign of being logged in
app
解決方案:在main.php
組件中修改user
'user' => [ 'class' => 'app\components\User', 'identityClass' => 'dektrium\user\models\User', ],