codeigniterframeworksconfigenvironmentcodeigniter-4

CodeIgniter 4 config files for different environments


I'm setting up a prototype application in CodeIgniter 4 and I'm totally puzzled when it comes to having different configurations for different environments. I have the concept of having different config files (or versions of them) for the different environments and keeping the framework's default config files untouched, but I don't know if that may be the appropriate approach for CI4. My main concern is if further framework updates may end changing the default config files.

I get how to use the .env files regarding DB and environment setup but I think on going further and set i.e. different locales, set up Monolog with different logs, set up different filters, etc. But I'm not familiar with the standard procedure for CI4.

I'd really appreciate some guidance with this, thanks in advance!

I've tried creating environment folders inside app/Config adapting namespaces but CI4 won't pick up the different files depending on the environment.

    app/
    ├── Config/
    │   ├── development/
    │   │   ├── app.php
    │   │   ├── database.php
    │   │   ├── filters.php
    │   │   └── ...
    │   ├── production/
    │   │   ├── app.php
    │   │   ├── database.php
    │   │   ├── filters.php
    │   │   └── ...
    │   ├── testing/
    │   │   ├── app.php
    │   │   ├── database.php
    │   │   ├── filters.php
    │   │   └── ...
    │   ├── app.php
    │   ├── database.php
    │   ├── filters.php
    │   └── ...

Solution

  • For your information, you would normally not need to manually modify/edit the framework's core configuration properties. All scaler and array structured configuration properties are changeable/modifiable using the environment variables in the .env file. This special .env file has a higher precedence over the hard-coded values in the respective configuration classes. This nullifies your concern regarding loss of settings due to a framework update.

    In addition, you may create custom configuration classes for your other settings that are outside the scope of the core framework setup. Public properties of these custom configuration classes are also changeable/overridable using the .env file of the respective environments.

    Summary:

    Have alternating .env files whose settings will override the public scalar and array structured properties of your new custom and existing core config classes. 😀

    Reference

    Configuration Classes and Environment Variables

    If the prefix of a namespaced variable exactly matches the namespace of the configuration class, then the trailing part of the setting (after the dot) is treated as a configuration property. If it matches an existing configuration property, the environment variable’s value will replace the corresponding value from the configuration file. If there is no match, the configuration class properties are left unchanged. In this usage, the prefix must be the full (case-sensitive) namespace of the class.

    Config\App.forceGlobalSecureRequests = true
    Config\App.CSPEnabled = true