从不同的数据库登录 Yii2
Login from Different Databases Yii2
我想在 Yii2 基本应用程序中使用来自不同数据库的两个表来实现登录功能。
在登录视图中,我添加了一个新字段:
<?= $form->field($model, 'username') ?>
<?= $form->field($model, 'password')->passwordInput() ?>
<?= $form->field($model, 'choice') ?>
在LoginForm中,我修改为:
public function getUser() {
if ($this->_user === false && $this->choice == 1) {
$this->_user = User::findByUsername($this->username);
}
else if ($this->_user === false && $this->choice == 2) {
$this->_user = UserPerusahaan::findByUsername($this->username);
}
return $this->_user;
}
User.php有这个:
public static function getDb() {
return \Yii::$app->dblogin; // use the "db2" application component
}
public static function tableName() {
return 'pengguna';
}
UserPerusahaan.php 与 User.php 的不同之处在于:
/*public static function getDb() {
return \Yii::$app->dblogin;
}*/
public static function tableName() {
return 'perusahaan';
}
当我尝试登录时,它只是刷新了登录页面。
我在这里错过了什么?或者还有其他更好的实用方法吗?
编辑:
我尝试将此添加到 web.php 中的组件:
'user' => [
'class' => 'yii\web\User',
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
],
'userperusahaan' => [
'class' => 'yii\web\User',
'identityClass' => 'app\models\UserPerusahaan',
'enableAutoLogin' => true,
],
这对 LoginForm.php:
public function login() {
if ($this->validate()&& $this->choice == 1) {
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
}
else if ($this->validate()&& $this->choice == 2) {
return Yii::$app->userperusahaan->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
}
return false;
}
使用 $choice = 1 的登录有效,但使用 $choice = 2 仍然让我刷新登录页面。
如果您使用那种情况,请避免在 accessControl 中使用角色“@”。角色“@”只适用于 Yii::$app->user,所以如果你使用不同的组件登录(例如 Yii::$app->userPerusahaan->login()),它不会算作具有角色的注册用户“ @”。像这个例子一样修改了你的 siteController。
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['index', 'login'],
'allow' => true,
'roles' => ['?'],
],
[
'actions' => ['logout'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
public function actionIndex()
{
if(Yii::$app->user->isGuest && Yii::$app->userPerusahaan->isGuest) return $this->redirect(['login']);
// ......
我想在 Yii2 基本应用程序中使用来自不同数据库的两个表来实现登录功能。
在登录视图中,我添加了一个新字段:
<?= $form->field($model, 'username') ?>
<?= $form->field($model, 'password')->passwordInput() ?>
<?= $form->field($model, 'choice') ?>
在LoginForm中,我修改为:
public function getUser() {
if ($this->_user === false && $this->choice == 1) {
$this->_user = User::findByUsername($this->username);
}
else if ($this->_user === false && $this->choice == 2) {
$this->_user = UserPerusahaan::findByUsername($this->username);
}
return $this->_user;
}
User.php有这个:
public static function getDb() {
return \Yii::$app->dblogin; // use the "db2" application component
}
public static function tableName() {
return 'pengguna';
}
UserPerusahaan.php 与 User.php 的不同之处在于:
/*public static function getDb() {
return \Yii::$app->dblogin;
}*/
public static function tableName() {
return 'perusahaan';
}
当我尝试登录时,它只是刷新了登录页面。 我在这里错过了什么?或者还有其他更好的实用方法吗?
编辑:
我尝试将此添加到 web.php 中的组件:
'user' => [
'class' => 'yii\web\User',
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
],
'userperusahaan' => [
'class' => 'yii\web\User',
'identityClass' => 'app\models\UserPerusahaan',
'enableAutoLogin' => true,
],
这对 LoginForm.php:
public function login() {
if ($this->validate()&& $this->choice == 1) {
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
}
else if ($this->validate()&& $this->choice == 2) {
return Yii::$app->userperusahaan->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
}
return false;
}
使用 $choice = 1 的登录有效,但使用 $choice = 2 仍然让我刷新登录页面。
如果您使用那种情况,请避免在 accessControl 中使用角色“@”。角色“@”只适用于 Yii::$app->user,所以如果你使用不同的组件登录(例如 Yii::$app->userPerusahaan->login()),它不会算作具有角色的注册用户“ @”。像这个例子一样修改了你的 siteController。
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['index', 'login'],
'allow' => true,
'roles' => ['?'],
],
[
'actions' => ['logout'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
public function actionIndex()
{
if(Yii::$app->user->isGuest && Yii::$app->userPerusahaan->isGuest) return $this->redirect(['login']);
// ......