Multiple identities in the frontend

Hi guys,

I started working with Yii2 since few weeks and installed the advanced environment of Yii2 with front- and backend.

My destination is to realisize a frontend login with customers- and suppliers-users.

Because of this I created 2 tables called "customers" and "suppliers" with a different number ob columns. And a model for each table called "customer" and "supplier".

After a long time of research I generated the two classes "CustomerIdentity" and "SupplierIdentity" with extends to yii/web/User. Then I write in the config.php this lines (instead of the common User-Component):

'customer' => [

            'identityClass' => 'frontend\models\CustomerIdentity',

            'class' => 'common\models\User',

            'enableAutoLogin' => true,

            'enableSession' => true,


        'supplier' => [

            'identityClass' => 'frontend\models\SupplierIdentity',

            'class' => 'common\models\User',

            'enableAutoLogin' => true,

            'enableSession' => true,


After a refresh I get the error "User::identityClass must be set." and I dont know what I can do to resolve it.

And I dont know how the LoginForm-Model must be configurate.

Can someone help me?

See if this wiki can be of any use to you.

Thanks for the fast support, but the given topic doesnt describe my situation. I use only frontend (at first) with two types of users and I dont use the session component (in the config.php).

It is beyond my understanding. Hope you will get some support from others.

I don’t think it’s possible to use multiple user identity classes in an application.

You’d better create a single “User” model (and the underlying “user” table) that holds all the necessary attributes for both Customer and Supplier, with an attribute (a column) that specifies the user type. If you don’t want to have many redundant columns, then you may consider separating those user type specific columns to other related tables (“customer” and “supplier”).

Yes of course - you are right. In my last project, I have one table for both, but this new project-"upgrade" I want programming more professionell und more flexible for the future.

Is it so complex to realize that? I thought Yii has all equipments for all situations without much expense.

PS: Sorry, that I dont answer yesterday, but this forum dont allow me to write a third post.

No problem. There are some restriction on the new comers in order to prevent possible spamming.

The point here is that we need to have a single ‘user’ application component (yii\web\User) that has a single ‘UserIdentity’ class. This is the schema with which Yii 2 handles the user authentication. All the users of your application must have an unique ID, no matter what type of user they might be. Multiple ‘user’ components or ‘UserIdentity’ classes are not in the basic assumption.

Ok, I understand.

Do you mean I should use the integrated roles of Yii to realize that?

Today, I research that the module "RBAC" gives me an option to generate some roles with user defined rules for special users.

I try it out step by step with this video-tutorial:

At this time I have the problem to understand the functionality of yii2-admin module, because I cant generate new roles for my users.

Yes, I think you definitely need to learn how to use RBAC.

I’m using “yii2-admin” in my projects, too. I can recommend this extension. It’s a simple and straightforward web UI for managing yii2 RBAC hierarchy. I like its simpleness. Also the idea of access control filter using “route permissions” is very convenient.

But, well, I don’t use “yii2-admin” to create/update RBAC hierarchy. I just use it to check and see how my RBAC hierarchy is constructed, and to assign roles to the users. I create/update roles and permissions through “migrations” because I want to have the source code of my RBAC hierarchy.

In your case, you may consider creating default roles of "customer" and "supplier" based on the user type. Or you may also introduce some administrative roles to manage the system.

Ok, thank you for your advice.

You see that I need more experience in that framework but I like it since the first day I’m working with Yii :)