Skip to content

Instantly share code, notes, and snippets.

@lsemel
Created November 21, 2010 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lsemel/709120 to your computer and use it in GitHub Desktop.
Save lsemel/709120 to your computer and use it in GitHub Desktop.
Vary configuration in CodeIgniter based on the the subdomain, e.g. (dev|staging|www).yourdomain.com
/**
|--------------------------------------------------------------------------
| 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',
);
<?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() == '' );
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment