Setting up the Codeigniter environment

Codeigniter development environment is not installed. I always use this code in index.php. but I don’t understand why I get “production” as output while I work on localhost.

switch(dirname(__FILE__)){ case "H:\wamp\www\sitedirectory": define('ENVIRONMENT', 'development'); break; default: define('ENVIRONMENT', 'production'); break; } echo ENVIRONMENT ; // output is "production" while i am on localhost echo dirname(__FILE__) ; // output is "H:\wamp\www\sitedirectory" 
+6
source share
3 answers

This is strange. He did the same 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 
+13
source

To dynamically set ENVIRONMENT based on the server’s IP address, below I used a regular expression to check the local IP address, such as 127.0. * and 10.0. *.

At the root of your project, look at 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/"); } 

Be sure to replace the BASEURL value with your own and add in application/config/config.php :

 $config['base_url'] = BASEURL; 

To improve further addition 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' => '' ]; } 
+1
source

Adding to other answers. Now the answer below may look like redundant (if you need to define environment variables, why use HTTP_HOST at all? In my experience, CI does not reflect any changes made to environment variables even after restarting apache. Updated values ​​when sending a request from the CLI.)

 if (php_sapi_name() === 'cli') { // incase the request is made using the cli, the $_SERVER['HTTP_HOST'] will not be set define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development'); } else { switch ($_SERVER["HTTP_HOST"]) { case "localhost": define('ENVIRONMENT', 'development'); break; default: define('ENVIRONMENT', 'production'); break; } } 
+1
source

Source: https://habr.com/ru/post/958145/


All Articles