Codeigniter Environment setting
That's strange. It did the exact same thing for me. Could you try something like this?
switch($_SERVER["HTTP_HOST"]){
case "localhost":
define('ENVIRONMENT', 'development');
break;
default:
define('ENVIRONMENT', 'production');
break;
}
echo ENVIRONMENT ; // output development
To dynamically set the ENVIRONMENT based on the server's IP, below I have used regular expression to check for local IP's such as 127.0.* and 10.0.*.
At the root of you project look to index.php
and replace:
define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');
with:
$server_ip = getHostByName(getHostName());
if (preg_match("/^(127\.0\.|10\.0\.).+/i", $server_ip)) {
define("ENVIRONMENT", "development");
define("BASEURL", "http://localhost:8000/");
} else {
define("ENVIRONMENT", "production");
define("BASEURL", "https://domain.com/");
}
Make sure to replace the value from BASEURL
with your own and in application/config/config.php
add:
$config['base_url'] = BASEURL;
To improve further add to application/config/database.php
right before the database settings $db['default'] = array(
:
if(ENVIRONMENT !== 'production') {
$db = [
'username' => '',
'password' => '',
'database' => '',
'hostname' => '127.0.0.1'
];
} else {
$db = [
'username' => '',
'password' => '',
'database' => '',
'hostname' => ''
];
}