public
Last active

Vary configuration in CodeIgniter based on the the subdomain, e.g. (dev|staging|www).yourdomain.com

  • Download Gist
MY_config.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
<?php
 
class MY_Config extends CI_Config {
/**
* Loads in custom server-specific configurations
*
* Additional config variables set
* server_name - the part of the hostname to the left of '.topleveldomain.com'
*
*/
public function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE) {
parent::load($file,$use_sections,$fail_gracefully);
$server_name = $this->server_name();
$this->config['server_name'] = $server_name;
if (array_key_exists('servers',$this->config)) {
$server_configs = $this->config['servers'];
if (array_key_exists($server_name,$server_configs)) {
while(list($k,$v) = each($server_configs[$server_name])) {
$this->config[$k] = $v;
}
}
unset($this->config['servers']);
}
}
public function dump() {
print "<pre>";
print_r($this->config);
print "</pre>";
}
 
 
/**
* Returns the portion of the domain before the [domain_name].[tld]
* @return string
*/
public function server_name() {
$server = $_SERVER['SERVER_NAME'];
$parts = explode('.',$server);
array_pop($parts); # .com
array_pop($parts); # domain
$server = join('.',$parts);
if (!array_key_exists('servers',$this->config)) {
return $server;
}
$equiv = $this->config['servers']['equiv'];
if (array_key_exists($server,$equiv)) {
$server = $equiv[$server];
}
return $server;
}
 
function is_localhost() {
return ($this->server_name() == 'localhost' || substr($this->server_name(),0,5) == 'local');
}
function is_live() {
return ($this->server_name() == 'www' || $this->server_name() == '' );
}
}
 
?>
config.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/**
|--------------------------------------------------------------------------
| In config.php, you can now use server-based configuration
|--------------------------------------------------------------------------
*/
 
/**
|--------------------------------------------------------------------------
| Translation table of equivalent server names. For instance, configure
| yourdomain.com and www2.yourdomain.com the same as www.yourdomain.com
|--------------------------------------------------------------------------
*/
$config['servers']['equiv'] = array(
'' => 'www',
'www2' => 'www',
);
 
/**
|--------------------------------------------------------------------------
| Keys and values specific to each domain
| You can continue to set other config values normally, and they
| will apply to all configurations
|--------------------------------------------------------------------------
*/
 
$config['servers']['local.dev'] = array(
'key1' => 'value1',
);
 
$config['servers']['dev'] = array(
'key1' => 'value1',
);
 
$config['servers']['staging'] = array(
'key1' => 'value1',
);
 
$config['servers']['www'] = array(
'key1' => 'value1',
);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.