Variable (array) available to each controller

I am new to yii.

I am using more than 1 controller in my website and each controller has few actions.

I want to use some variables across each controller (Value of variable will be fixed, I need some constants for a formula). Whats the best place (standard way) to define those variables ? Should I use session ? (as value is not going to change).

if they are user-specific then use session. If they are rather configuration parameters use application params (defined in config file, which you can access with Yii::app()->params[‘xxx’])

You can do it multiple ways -

  1. Define a Class of your own which overrides the Controller class and then let all your controllers extend that custom class. Within that class, you can define all your constants.

  2. Define the contants as a ‘param’ in your configuration file (main.php in your case i suppose).

Thanks for the help. I was thinking about the params too.

php as such does not support multiple inheritances. There are a few tricks to overcome that. You can google for them. But why would you inherit both your class and the controller class? why not just let your base class inherit the controller class and be done away with it? Multiple inheritance is normally a call to maintenance problems in the long run.

I googled before your answer came, and so deleted my reply (Not supported and a not a nice move to use multiple inheritance)

But one more question, since any controller/action can ‘get’ that variable, where should I ‘set’ that variable (I am going to use params ) i.e. which is the starting point on yii (the page load event ?) . So, that I can make sure the variable is already ‘set’ before it is being used ?

in your main.php file (in config folder). main.php returns an array before the applications starts. (checkout index.php in your application folder to see the flow before everything starts).

Thanks again for your time.

For anyone who has similar question, I am using the way which is given in a demo app by yii.

In main.php,

‘params’=>require(dirname(FILE).’/params.php’)

And a separate file params.php which returns an array of constants. I liked this approach.