CodeIgniter Forums
User agent code for entire application - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: User agent code for entire application (/showthread.php?tid=5152)



User agent code for entire application - El Forum - 01-08-2008

[eluser]bapobap[/eluser]
Hi there,

Quick question. I have about 4 lines of code that checks if the user agent is a mobile device or a normal browser. It then sets a variable, which I use to include the files necessary to build the view.

My entire application would need to be able to "see" this var but I'm not sure where to put the code so that the var is set when a page is requested then available to all the code throughout my app.

Does anyone have a suggestion on where the best place to put this (and similar code) is? Sorry if I'm not making sense!

Thanks!


User agent code for entire application - El Forum - 01-08-2008

[eluser]tonanbarbarian[/eluser]
if you set the value you want in the config object you will be able to access it anywhere
Code:
$this->config->set_item('my_value', $value);
then to read it
Code:
$this->config->item('my_value');
Just make sure that the name you use is unique so that it does not overwrite a core CI config value


User agent code for entire application - El Forum - 01-08-2008

[eluser]bapobap[/eluser]
Thanks for that, should I put that line in a certain file, or does it not matter where I put it?

Thanks


User agent code for entire application - El Forum - 01-08-2008

[eluser]tonanbarbarian[/eluser]
It does matter to a certain degree where you put it
in a controller or model
Code:
$this->config->set_item('my_item', $value);
...
$this->config->item('my_item');
In a library, or if you must, a view
Code:
$CI =& get_instance();
$CI->config->set_item('my_item', $value);
...
$CI->config->item('my_item');
Just be careful, if you are extending some of the core CI libraries, like Input, Router, Benchmark etc they may be instantiated before the controller is created in which case neither of the 2 methods above will work


User agent code for entire application - El Forum - 01-08-2008

[eluser]bapobap[/eluser]
Thanks for your help!