3
votes

I am trying to setup Yii2 advanced like a traditional user/admin system. Frontend would be /user and backend would be /admin, and would use their respective table in the database (user and admin). I have not renamed frontend and backend to user and admin yet..

Using migrate generated the 'user' table, with all it's fields. I registered to create a new user, all that works perfect. I then copied the 'user' table and named it 'admin', and changed the username to admin. I can change the password, or truncate it, register new admin user, then remove the registration from the backend later. The admin table in the db itself isn't the issue as I am not getting that far when I reach the error..

I have setup and used Yii2 advanced just fine on the frontend (user) side of it. Of course, you have Yii::$app->user and it works just fine on the frontend. I can login, it uses the 'users' table. Frontend works great...

Now on the backend (admin) I need it to use the 'admin' table. I know you specify the table to use in the model. I copied /common/models/User.php and have /common/models/Admin.php and updated the function to use the 'admin' table instead.

I also copied /vendor/yiisoft/yii2/web/User.php and put it in /common/models/web/Admin.php (and renamed the name of the class from User to Admin)

Then I edited the /backend/config/main.php to reflect the changes for Admin (class and identityClass).


/backend/config/main.php

'components' => [
    'admin' => [
        'identityClass' => 'common\models\Admin',
        'class' => 'common\models\web\Admin',
        'enableAutoLogin' => true,
    ],
],

/common/models/web/Admin.php

class Admin extends Component { ... }

/common/models/Admin.php

class Admin extends ActiveRecord implements IdentityInterface {
    public static function tableName()
    {
        return '{{%admin}}';
    }
}

Error: User::identityClass must be set. <-- As you can see, it's still references the User model some how...

Also, when I get this setup, would I use Yii::$app->admin instead of Yii::$app->user ? Like for checking if they are logged in using isGuest.

I want to be sure that a user can't login to frontend, then manually go to backend and be logged in!

1

1 Answers

3
votes

I have solved this :)

You have to edit the main config of each (frontend and backend) and specify the 'identityClass' for the user component, and add 'session' and 'request' to the list.

Example of frontend config:

'components' => [
    'user' => [
        'identityClass' => 'common\models\User',
        'enableAutoLogin' => true,
        'identityCookie' => [
            'name' => '_frontendUser', // unique for frontend
        ]
    ],
    'session' => [
        'name' => 'PHPFRONTSESSID',
        'savePath' => sys_get_temp_dir(),
    ],
    'request' => [
        // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
        'cookieValidationKey' => '[RANDOM KEY HERE]',
        'csrfParam' => '_frontendCSRF',
    ],
],

Example of backend config:

'components' => [
    'user' => [
        'identityClass' => 'common\models\Admin',
        'enableAutoLogin' => true,
        'identityCookie' => [
            'name' => '_backendUser', // unique for backend
        ]
    ],
    'session' => [
        'name' => 'PHPBACKSESSID',
        'savePath' => sys_get_temp_dir(),
    ],
    'request' => [
        // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
        'cookieValidationKey' => '[DIFFERENT UNIQUE KEY]',
        'csrfParam' => '_backendCSRF',
    ],
],

For a more detailed guide, you can read the wiki I created.

Wiki: [Guide] How to actually separate Frontend and Backend on Yii2 Advanced