I developing a site with two different registrations, and I have 2 different table, Im using RbacDB, and in the web config in the components section I have user configuration, according to this I want to know how I can use 2 different fields in the config file?
config :
'components' => [
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => '213h2i3121h12osiajls',
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
],
// Here after user I need to add another config user-two
'user-two' => [
'identityClass' => 'app\models\SecondUser',
'enableAutoLogin' => true,
],
when I do it, shows this error enter image description here
Thanks!
You have to create a web user class for the second identity
than specify the class name in your config
Try to set a class property in the user-two component:
or create new class inherited from the yii\web\User class and set like this:
Maybe this will help you.
I have gone through the yii2 framework internals. As I have understood, You can make N Identities following below technique;
N identities are very useful when you do not want to implement Complex RBAC (Role Based Access Control) and just want to Filter Access at controller's request.
Let us Assume I have to create yet another Identity called 'Franchise' other than existing User which is nicely coupled inside Yii2 Framework.
DB Migrations
Create a new migration file using command
Copy paste the content of already available migration file at location PROJECT_NAME\console\migrations something like 'm170311_105858_create_user.php' and rename table name from 'user' to 'franchise'.
Now, run migration command
You must get something like this on command prompt
check DB whether DB is created. (I assume you have made DB settings in PROJECT_NAME\common\config\main-local.php)
Creating Franchise Model
Just goto 'Gii' Module and create a model for newly create franchise table.
Model location must be PROJECT_NAME\common\models\Franchise.php
Make Sure that the Model class implements IdentityInterface and also implements mandatory methods of IdentityInterface
Identity Class
If you go to location PROJECT_NAME\vendor\yiisoft\yii2\web\User.php. This is the class that is referred everywhere in your project as Yii::$app->user. Copy paste the content of this class and create a new File called PROJECT_NAME\vendor\yiisoft\yii2\web\Franchise.php and paste the content in it. Make below changes in the file.
PROJECT_NAME\vendor\yiisoft\yii2\web\Application.php
In Application.php add below method,
Also find method coreComponents() and add one more entry as below,
PROJECT_NAME\frontend\config\main.php
Inside components add below entry just after 'user' entry,