• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
User_agent in my_config.php?

#1
[eluser]ThatAirplaneGuy[/eluser]
I would like to be able to detect the user's platform via user_agent library during config load. Depending on what platform the user is running, I will set an 'ENV_TYPE' config variable that I will access throughout the application to create different views, menus, functionality, etc.

I know I can do the user_agent detection in the default controller, but if I link to a separate controller and bypass the default controller, I would bypass the platform detection and would need the platform detection in every one of my controllers. I would rather have it done at the core application level, and let the config variable be accessible by all controllers afterwards.

I have the user_agent library in autoload, but I can't access the library in config.php.

Am I missing an easy fix? Any ideas?

Thanks ahead of time...
CJ

#2
[eluser]ThatAirplaneGuy[/eluser]
Well, I found a work-around.

Hard-code in config.php:
Code:
if(!$config['IS_DEV']){
    require_once('system/libraries/User_agent.php');
    $ua = new CI_User_agent();
    if($ua->platform() == 'Windows XP'){
        $config['ENV_TYPE'] = 'PC';
    }else{
        $config['ENV_TYPE'] = 'RF';
    }
}else{
    $config['ENV_TYPE'] = $config['DEV_TYPE'];

In my mind, it isn't ideal to hard-code a 'require' statement in the config file, so if anyone has a better way to get the same result, I'm open to suggestions.

CJ

#3
[eluser]InsiteFX[/eluser]
Code:
$ua = $_SERVER['HTTP_USER_AGENT'];

InsiteFX

#4
[eluser]ThatAirplaneGuy[/eluser]
[quote author="InsiteFX" date="1305072776"]
Code:
$ua = $_SERVER['HTTP_USER_AGENT'];

InsiteFX[/quote]

Thanks for the reply.

I was hoping to find a way to do it using only CI functions.

As it turns out, I decided to go a different direction, so I'm not even going to need this at all now...

Thanks,
CJ

#5
[eluser]Nick_MyShuitings[/eluser]
Just a question... but why would you have a preference for using only CI functions over plain PHP (when the answer is the difference between loading an entire new class and using a PHP function).

secondly, for sake of future readers of the forum, any details in your final solution are welcomed... if nothing else to give closure to the thread.

#6
[eluser]ThatAirplaneGuy[/eluser]
[quote author="Nick_MyShuitings" date="1305109635"]Just a question... but why would you have a preference for using only CI functions over plain PHP (when the answer is the difference between loading an entire new class and using a PHP function).

secondly, for sake of future readers of the forum, any details in your final solution are welcomed... if nothing else to give closure to the thread.[/quote]

No real reason for not mixing PHP with CI. If nothing else - just to keep the same look of the rest of the code. Once I get this project off the ground, I will be working with folks that have never used CI before and are barely experienced with PHP. Anything I can do to keep it all it one 'realm' - all PHP 'core' code or all CI code - would make it easier to get them up to speed. It prevents having to explain "No, that is not a CI function, it is a PHP function. No that is not a PHP function, it is a CI function." etc. etc. There is no functional reason for preferring not to mix the code.

My end solution for the project was to split my application out into two separate projects. I was trying to build a 'mobile' version of the application - complete with separate templates, etc. - in the same classes as a PC version of the application. I eventually decided that any 'pros' of having one set of code were far outweighed by not having jumbled mixed-environment code (again, going back to simplicity for the other people that will be working on the project). It also turned out that there wasn't a lot of overlap of functionality between the mobile vs. pc based versions.

Is it possible to do a mixed-environment project with CI? Definitely. It just wasn't the best scenario for my project this time.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.