Now even when the user is logged in to the Member Module, the Login link still gets displayed on the main home page. I assume this is because Yii::app()->user->isGuest does not check the member module. How to fix this?
Ahh I see, you have to realize Yii::app() is a module itself. If you do Yii::app()->setComponents(), your original components defined in the main config under the base componenents array get overwritten.
This could lead to the following:
echo Yii::app()->user->id; // 1
$module = Yii::app()->getModule('member'); // init method of MemberModule get called and user component get changed
echo Yii::app()->user->id; // 2
If you don’t mixup the different modules (access one module in another), you don’t run in any problems with your approach though.
I think the right solution in your case is to define the user component in the main config (like explained) and the errorHandler component in the beforeControllerAction method of the MemberModule:
public function beforeControllerAction($controller, $action)
{
if (parent::beforeControllerAction($controller, $action))
{
Yii::app()->setComponents(array(
'errorHandler'=>array(
'errorAction'=>'member/default/error',
),
));
return true;
}
}
This way, the errorHandler gets overwritten only when a actual member module controller is running.
// Note that within the member module, you should not access the user object via Yii::app()->user. See my first post in this thread. Though if you don’t like that, you are free to overwrite the user object in the beforeControllerAction method as well.
Means if you access Yii::app()->user below this line, you would access the user object defined in init() method, not the one in the main config under the base components array.
You can do that. But you may need to put the loginUrl in both user component configurations. If you only set it for the user component of the member module, you will get something different when doing this within a controller which is not part of the member module:
Well in main config, just put the loginUrl 2 times. First time for the main user component and the seconds the for the member module user component. Otherwise you may run into problems.