Welcome Guest, Not a member yet? Register   Sign In
User agent code for entire application
#1

[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!
#2

[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
#3

[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
#4

[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
#5

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




Theme © iAndrew 2016 - Forum software by © MyBB