Variable (array) available to each controller
Posted 15 April 2012 - 01:58 PM
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).
Posted 15 April 2012 - 02:40 PM
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).
Posted 15 April 2012 - 03:23 PM
Posted 15 April 2012 - 03:30 PM
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 ?
Posted 15 April 2012 - 03:42 PM
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.
And a separate file params.php which returns an array of constants. I liked this approach.