CodeIgniter Forums
Where to place general global variables? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Where to place general global variables? (/showthread.php?tid=27783)

Pages: 1 2


Where to place general global variables? - El Forum - 02-20-2010

[eluser]chefnelone[/eluser]
Hello,

I use to create some variables which are available in the entire app.
As a example:
$clientsEmail = '[email protected]';
$contactFormEmail = '[email protected]';

I simply create these vars in php file: myVars.php , and then I include this file in the header of every page.


But I'm sure that CI offers a cleaner way to do this to avoid including files.
I mean, creating those vars in config.php or autoload.php, etc.

How do you do this?


Where to place general global variables? - El Forum - 02-20-2010

[eluser]Ki[/eluser]
I have had the same problem - I had variables + DB requests that needed to be defined on every page. To accomplish this I have created My_controller (as suggested by multiple CI posts). All other controllers extend My_controller instead of Controller. This way, My_controller holds everything that needs to be done for every page on the site. If you have a section that does not need that stuff, simply extend Controller.
As for variables that are used everywhere, I simply defined them in My_Controller as constants. If you want to define variables that are defined regardless of My_controller or controller being loaded, you may look into hooks, or define them in your config.php - but that is a it controversial


Where to place general global variables? - El Forum - 02-20-2010

[eluser]ciGR[/eluser]
Have you see that http://ellislab.com/codeigniter/user-guide/libraries/config.html


Where to place general global variables? - El Forum - 02-20-2010

[eluser]Ki[/eluser]
Another good point!


Where to place general global variables? - El Forum - 02-20-2010

[eluser]JoostV[/eluser]
1. Store them in a custom config file as $config['clientsEmail']; and so on.
2. Store the custom config file in the config folder under a custom name
3. Load the custom config file and call the settings
Code:
$this->config->load('myconfigfilename');
$this->config->item('clientsEmail');

Its all in the user guide Wink


Where to place general global variables? - El Forum - 02-20-2010

[eluser]chefnelone[/eluser]
Since I'll need the variables everywhere at everytime, I think that a custom config file can do it. The user guide is quite clear about how to do it, but.. can a config file load data from a database?... If not, I might look for a 'hook' to do this.


Where to place general global variables? - El Forum - 02-20-2010

[eluser]Zeeshan Rasool[/eluser]
Not sure about dynamic data from db to config but may be possible if we create a CI instance as we create in libraries or in hooks. You can also create a custom config type file in which you declare all your variables and just called them any where with: $this->config->load('email');


Where to place general global variables? - El Forum - 02-20-2010

[eluser]Ki[/eluser]
I don't mean to be repeating what I mentioned earlier, but if you load info from database, why don't you use My_controller to load everything you need for every page? It seems to be the common solution judging by the posts...


Where to place general global variables? - El Forum - 02-21-2010

[eluser]chefnelone[/eluser]
[quote author="samota" date="1266715168"]I don't mean to be repeating what I mentioned earlier, but if you load info from database, why don't you use My_controller to load everything you need for every page? It seems to be the common solution judging by the posts...[/quote]
I think that since a controller can't be autoloaded it isn't a good option for what I need.
I need the variables available everytime/everypage.


Where to place general global variables? - El Forum - 02-21-2010

[eluser]Ki[/eluser]
[quote author="chefnelone" date="1266802139"][quote author="samota" date="1266715168"]I don't mean to be repeating what I mentioned earlier, but if you load info from database, why don't you use My_controller to load everything you need for every page? It seems to be the common solution judging by the posts...[/quote]
I think that since a controller can't be autoloaded it isn't a good option for what I need.
I need the variables available everytime/everypage.[/quote]

If you create My_controller, will just extend it every page instead of the Controller. This way you do not have to load library/model in every controller. Best of luck!