Search code examples
phpcodeignitercodeigniter-4

How to define baseURL based on ENVIRONMENT in CodeIgniter 4


Upgraded from CodeIgniter 3 to CodeIgniter 4 and at the begining I found a problem I cannot resolve. In CI3 I had a switch statement in application/config/config.php to set the $config['base_url']. It's like

$localFolder = "localfolder";

switch (ENVIRONMENT) {
    case 'development':
        $config['base_url'] = "http://$localFolder.loc";
        break;

    default:
        $config['base_url'] = "http://testserver.com/$localFolder";
        break;
}

But in CI4 the app/Config/App.php is now a class and I haven't figured out how could I define the public $baseURL = "samplefolder"; based on the ENVIRONMENT variable.

Calling a function immediately not working:

public $baseURL = (function(){
        switch (ENVIRONMENT) {
            case 'development':
                $this->baseURL = "http://$localFolder.loc";
                break;

            default:
                $this->baseURL = "http://testserver.com/$localFolder";
                break;
        }
    })();

Error:

Fatal error: Constant expression contains invalid operations

Also, calling the function after declaring with $this-> produces error:

public $baseURL = "";

public function baseURL($localFolder)
    {
        switch (ENVIRONMENT) {
            case 'development':
                $this->baseURL = "http://$localFolder.loc";
                break;

            default:
                $this->baseURL = "http://testserver.com/$localFolder";
                break;
        }
    }

$this->baseURL("localfolder");

Error:

Parse error: syntax error, unexpected '$this' (T_VARIABLE), expecting function (T_FUNCTION) or const (T_CONST)

I would appreciate any help!


Solution

  • Good question and as I haven't yet looked into doing something similar for my own sites, now is as good as time as any to look into this...

    This isn't the greatest of solutions but something to consider.

    You could add (append) the following to you App.php to contain...

        protected $localFolder = 'localfolder';
    
        public function __construct() {
            parent::__construct();
    
            $this->setBaseUrl(); // Set the Base URL
        }
    
        protected function setBaseUrl() {
            switch ($_ENV['CI_ENVIRONMENT']) {
                case 'development':
                    $this->baseURL = "http://$this->localFolder.loc";
                    break;
                default:
                    $this->baseURL = "http://testserver.com/$this->localFolder";
                    break;
            }
        }
    } // End of APP Class
    

    So changing your CI_ENVIRONMENT Value in your .env file will switch your $baseURL.

    A Better way

    You might be better off setting your $localFolder as a ENV value so you can control this from the one location.

    LOCAL_FOLDER = 'localfolder'

    Within your .env file

    #--------------------------------------------------------------------
    # ENVIRONMENT
    #--------------------------------------------------------------------
    
    # CI_ENVIRONMENT = production
    CI_ENVIRONMENT = development
    LOCAL_FOLDER = 'localfolder'
    

    Then the setBaseUrl method would become

    protected function setBaseUrl() {
        switch ($_ENV['CI_ENVIRONMENT']) {
            case 'development':
                $this->baseURL = "http://{$_ENV['LOCAL_FOLDER']}.loc";
                    break;
            default:
                $this->baseURL = "http://testserver.com/{$_ENV['LOCAL_FOLDER']}";
                    break;
        }
    }
    

    Hoepfully that gives you a few ideas.