Module based login

I have 3 modules customer, admin, dealers i need to separate login/logout function for 3 modules,

What configuration/code i need to use? any detailed examples welcome

Let’s take admin module

Inside your admin/AdminModule.php file, add the following line at function ‘init’:


This add a specific preffix referring to your module.

If you want to define a specific login action for your module, add the following line

Yii::app()->user->loginUrl = Yii::app()->createUrl("/{$this->id}/default/login");

So, define your login action at admin/controllers/DefaultController.php, function actionLogin.

At DefaultControler/actionLogout, logout function must the added as below:


Thanks for your reply,

every thing works fine but if i logged in as admin and without logout if i typed customer/default/profile it show a profile for customer how do i change this?

I copied UserIdentity components to module components folder because i have different login table for different module users

In module configuration i have the following line


        'errorHandler' => array(

            'errorAction' => 'customer/default/error'),

        'user' => array(

            'class' => 'CWebUser',

            'stateKeyPrefix' => '_customer',

            'loginUrl' => Yii::app()->createUrl('customer/default/login'),


I didn’t understand it very well.

If I am at ‘admin’ module, I will use admin profile

If I am at ‘customer’ module, I will use customer profile

i am logged in as a admin, and open a new tab and enter customer/default/profile in addressbar i can able to view customer profile even though i am not logged in as a customer user

i can able to view customer profile without logged in as a customer user

I found the problem, the problem is stateKeyPrefix in config section not worked

if i set stateKeyPrefix via method it works perfectly


this line rocks!!!

i solved this issue and added a new wiki article on this site

Hm, really!! I wasn’t understanding why it was not working! So that was it!

Reply by suriyansuresh:

But this is just a workaround - how come it does not redirect to the correct login page in the first place?

Better aproach is doing a

Yii::app()->user->loginUrl = Yii::app()->createUrl("/{$this->id}/default/login");

at every xxxModule you need.

Therefore, all this is a workaround. Yii is not oriented to multiple logins.

You can also configure multiple CWebUsers if that helps:

Thanks, Mike! Maybe configuring multiple CWebUsers would be an even better approach.

Can you confirm that errorAction is working for you? It seems to ignore it in my module config and uses the default ‘site/error’ action. If I change $this->setComponents to Yii::app()->setComponents then it works.