Difference between #6 and #7 of
How to login from different tables in Yii2

Revision #7 has been created by samdark on Apr 5, 2018, 1:57:25 PM with the memo:

Improved formatting
« previous (#6) next (#8) »

Changes

Title unchanged

How to login from different tables in Yii2

Category unchanged

How-tos

Yii version unchanged

2.0

Tags unchanged

authentication,login,different table name,multiple login

Content changed

[...]
Solution:
Create instances in web.php to uses UserIdentify.
eg:

```php
  $user = \Yii::$app->user;   $school = \Yii::$app->school;   $teacher = \Yii::$app->teacher;
```
[...]
```php

 
     
'user' => [              'class'=>'yii\web\User',              'identityClass' => 'app\models\User',              'enableAutoLogin' => false,              'authTimeout' => 60*30,              'loginUrl' => ['dashboard/login'],              'identityCookie' => [                  'name' => '_panelUser',             ]
 
        ],
 
        
]
 
],
 
'school'=>[              'class'=>'yii\web\User',              'identityClass' => 'app\models\SchoolUser',              'enableAutoLogin' => false,              'authTimeout' => 60*30,              'loginUrl' => ['dashboard-school/login'],              'identityCookie' => [                  'name' => '_panelSchool',             ]
 
        ],
 
        
]
 
],
 
'teacher'=> [              'class'=>'yii\web\User',              'identityClass' => 'app\models\TeacherUser',              'enableAutoLogin' => false,              'authTimeout' => 60*30,              'loginUrl' => ['dashboard-teacher/login'],          'identityCookie' => [                  'name' => '_painelTeacher',             ]
 
        
]
 
], ``` Note that for each there is a identifyClass and one view login.
Now, we need to create the models:
[...]
return '{{%user}}';
}
//
// to continues....
```
[...]
return '{{%schoolUser}}';
}
//
// to continues

```
[...]
return '{{%teacher}}';
}
//
// to continues....

```
[...]
public function behaviors()
{

 
    
return [
'access' => [
'class' => AccessControl::className(),
[...]
```php

 
//school  \Yii::$app->scholl->login($model, $this->rememberMe ? 3600*24*30 : 0);  //teacher  \Yii::$app->teacher->login($model, $this->rememberMe ? 3600*24*30 : 0);
 
```
[...]
```php

 
<?php if (!\Yii::$app->scholl->isGuest):?>
<h1>My scholl Name: <?=\Yii::$app->scholl->identity->name?>
<?php endif;?>
[...]
<h1>Teacher Name: <?=\Yii::$app->teacher->identity->name?>
<?php endif;?>

 
``` Always use the specific instance of the user you want to work with. Criticism, suggestions and improvements, use the comments
 
 
 
 
 
 
 
 
5 0
6 followers
Viewed: 64 347 times
Version: 2.0
Category: How-tos
Written by: AndroideLP
Last updated by: lenovo
Created on: Apr 3, 2018
Last updated: 4 years ago
Update Article

Revisions

View all history