CodeIgniter Forums
Set Environment via Config\App - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=31)
+--- Thread: Set Environment via Config\App (/showthread.php?tid=75114)



Set Environment via Config\App - Mostafa Khudair - 12-24-2019

Hello guys,
I was thinking of a simple way to change between supported framework environments, so i made this.

Step 1: we need to configure environment via \Config\App at App/Config/App.php file:
PHP Code:
<?php namespace Config;

use 
CodeIgniter\Config\BaseConfig;

class 
App extends BaseConfig
{

    
/**
     | You can load different configurations depending on your
     | current environment. Setting the environment also influences
     | things like logging and error reporting.
     |
     | This can be set to anything, but supported usage are:
     |
     |     development
     |     testing
     |     production
     */
    
public $environment 'development'
 
Step 2: we should add default supported environments list in system\CodeIgniter.php file:
PHP Code:
<?php namespace CodeIgniter;

class 
CodeIgniter
{
    protected 
$environment = [
        
'development',
        
'testing',
        
'production'
    
]; 

Step 3: we have to edit some codes to make it work as expected, search for detectEnvironment() function in system\CodeIgniter.php file:
PHP Code:
    protected function detectEnvironment()
    {
        
// Make sure ENVIRONMENT isn't already set by other means.
        
if (! defined('ENVIRONMENT'))
        {
            
// running under Continuous Integration server?
            
if (getenv('CI') !== false)
            {
                
define('ENVIRONMENT''testing');
            }
            else
            {
                
$config = new \Config\App;
                if (! 
in_array($config->environment$this->environment))
                {
                    die(
"The application environment <span style='color:red'>{$config->environment}</span> is not supported, currently supported enviroments are: " implode(", ",$this->environment) . '.');
                }
                
define('ENVIRONMENT'$_SERVER['ENVIRONMENT'] ?? $config->environment);
            }
        }
    } 

Thats all for me, I'm new in php world so I don't know what are the consequences.


RE: Set Environment via Config\App - InsiteFX - 12-26-2019

CodeIgniter 4 User Guide - Handling Multiple Environments