Welcome Guest, Not a member yet? Register   Sign In
Set Environment via Config\App
#1

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.
Reply
#2

CodeIgniter 4 User Guide - Handling Multiple Environments
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB