CodeIgniter Forums
make the config smart base on environment - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: make the config smart base on environment (/thread-37044.html)



make the config smart base on environment - El Forum - 12-24-2010

[eluser]newtonianb[/eluser]
I have several fields in config.php, database.php and constants.php that are different when i publish my site to server.
How can I make it smart and have it automatically pick the right one based on lets say hostname.


I tried if(strpos(base_url(),"localhost") === false)
but base_url() isn't working in these files, it actually seems to only work inside database.php

What else can I do?


make the config smart base on environment - El Forum - 12-24-2010

[eluser]CroNiX[/eluser]
[quote author="newtonianb" date="1293259879"]I have several fields in config.php, database.php and constants.php that are different when i publish my site to server.
How can I make it smart and have it automatically pick the right one based on lets say hostname.


I tried if(strpos(base_url(),"localhost") === false)
but base_url() isn't working in these files, it actually seems to only work inside database.php

What else can I do?[/quote]

In my index.php, at the top, I do:
Code:
define('LIVESITE', ($_SERVER['HTTP_HOST'] === strtolower('www.yoursite.com')) ? TRUE : FALSE);
Then, globally, you can:
Code:
if(LIVESITE)
{
    //something for the live site
}
else
{
    //something for your dev site
}
I also use it to define all of my config database setting and other variables, such as base_url based upon that setting.
Code:
$db['default']['username'] = (LIVESITE) ? 'the_live_site_username' : 'the_dev_site_username';
$db['default']['password'] = (LIVESITE) ? 'the_live_site_password' : 'the_dev_site_password';



make the config smart base on environment - El Forum - 12-24-2010

[eluser]newtonianb[/eluser]
fantastic! thanks alot!
last question how can I access $config['base_url'] from constants.php?


make the config smart base on environment - El Forum - 12-24-2010

[eluser]CroNiX[/eluser]
echo base_url(); should work

load the url helper

http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html


make the config smart base on environment - El Forum - 12-24-2010

[eluser]CroNiX[/eluser]
Although I'm not sure if that is accessible yet at that point. Try it.
You can always make a custom helper with your code and autoload that (with your custom define statements)