I want to do some config in config/main.php, but while I can get a handle on Yii::app() there, I cannot get a handle on Yii::app()->user (or Yii::app()->getUser()) – both return null, even when the user is logged in.
First, why is Yii::app()->user not available at this point?
Second, where should I be doing "global" configuration that requires user if not in config/main.php?
There are other ways to do this. For example, you could respond to the onBeginRequest event of the application (you can attach to this event in app config). You could also extend CWebApplication. Using base controller’s init() is the easiest way to go.
The reason we introduce init() is because the signature of constructors could be complex, and if you override the constructor, you must call the parent implementation. Overriding init() doesn’t have these requirements.
Thanks very much. I’ve gone with a base controller. Thinking about it, there have been times where I’ve needed an intermezzo class like this, so it looks like the right way to go to me too! I also think that it’ll be useful for testing.
I also hadn’t realised that init() is invoke by Yii, so I was wondering how it was used.
I’m not sure why calling the parent constructor is an issue here, as you still have to call parent::init() in the controllers’ init() when using a base controller. But I can see that the constructor signatures could be a bit of a mess, so I understand the approach taken – I’m a pragmatist not a purist.