Codigniter 4.6.1 not loading config files based on environment |
I have an environment, apart from development and production, called "sandbox", and I have created "Config/sanbox/Apx.php". Am able to access values in the "Config/Apx.php" but unable to Access values in the Config/sandbox/Apx.php, even when I set the environment to sandbox. I have also created Boot/sandbox.php. The environment loads correctly, but codeigniter does not load the sanbox configuration. Any help ? I need to have the sandbox configuration for the custom configuration.
Thanks <?php namespace Config\sandbox; use CodeIgniter\Config\BaseConfig; class Apx extends BaseConfig { public string $logo_url = 'img/sanbox_logo.png'; }
This type of config variable handling was supported in CodeIgniter 3, but is no longer available in CodeIgniter 4. Instead, you should use the .env file: https://codeigniter.com/user_guide/gener...vironments
You can create a Configuration class and use its properties, but it won't be loaded based on the environment. You'd have to figure out which environment is being used to achieve a similar result to what michalsn stated. For instance:
PHP Code: $config = config(match($_ENV['CI_ENVIRONMENT']) However, that's more difficult to maintain and not ideal. Like I stated, michalsn's approach makes the most sense, but I couldn't discount that there may be some need/desire to create a Configuration class.
So what am actually doing is more than logo based on environment. I want to use the config to hold entire app module settings. For example, in one instance, I may deploy 4 modules, in another instance 5 different modules. So I will have the config/instance/[files] handle the configurations that will be peculiar to that instance. The environment file will just hold minimal information like CI_environment. when the the config php files, I can obfuscate the PHP code for added security., example DB connection etc. We really should be able to load configuration based on environment or use the default as fall back.
(05-28-2025, 06:03 AM)chenzen Wrote: For example, in one instance, I may deploy 4 modules, in another instance 5 different modules. So I will have the config/instance/[files] handle the configurations that will be peculiar to that instance. What do you mean by "instance"? That’s not a built-in CodeIgniter concept. In CodeIgniter, each module can (and should if required) come with its own config file under the module's Config directory. If you're trying to build a kind of multi-tenant configuration system, where different deployments load different sets of modules or settings - that's not supported out of the box, and I don't believe it will be in the future. If you're aiming for dynamic or tenant-specific configurations, I’d suggest looking into Registrars or the Settings package. These give you more flexible and maintainable ways to manage application-level config at runtime.
Take a look at System/Boot...line 144
(new DotEnv($paths->appDirectory . '../'))->load(); This is not working, just tried a fresh download of 4.6.1. I suggest the code is updated to read from the Paths.php file as well. in the Config/Paths.php public string $env_directory = __DIR__ . '/../../'; Then in System/Boot.php (new DotEnv($paths->env_directory))->load(); This simple update would fix them problem and allow the framework to be used in creative ways. The env file used to be loaded from the index.php file, if it's put in the Paths, just like all the others, it will be helpful. Thanks (05-28-2025, 11:37 PM)chenzen Wrote: Take a look at System/Boot...line 144 Currently, it's a different line: 173, see: https://github.com/codeigniter4/CodeIgni...t.php#L173 (05-28-2025, 11:37 PM)chenzen Wrote: This is not working, just tried a fresh download of 4.6.1. I suggest the code is updated to read from the Paths.php file as well. What is not working? Loading .env file? (05-28-2025, 11:37 PM)chenzen Wrote: This simple update would fix them problem and allow the framework to be used in creative ways. The env file used to be loaded from the index.php file, if it's put in the Paths, just like all the others, it will be helpful. What creative way do you have in mind? Why do you want to modify the place of the .env file? Please explain the concept - why do you need this change, and what are you trying to achieve.
I download the ci4 from the codeigniter website, that version seem to be different from what you shared on the GitHub, but all the same...CodeIgniter is very powerful, thanks for the great work to have brought it to where it is.
In the version 4.4.6...the following code was in the index.php file require_once SYSTEMPATH . 'Config/DotEnv.php'; (new CodeIgniter\Config\DotEnv(ROOTPATH))->load(); For one of my applications, I was running the exact code base but with several databases, so I used the index.php file and the .env to determine the settings needed for each setup. It's a cloud based software, I maintain one source code and just provide different databases for ease of maintenance, and for added security, I kept the env files in a custom location, each client accessed the codebase via a sub domain. So multiple people accessing same code base but using different DB settings. Any reason it was taken out of there ? At the moment, am setting an environment using the env, then I use that to fetch appropriate config items from my Config/custom.php file, so am using arrays and "getters" to get the specific value related to that environment, which is not really the best, because I have to write several methods in the config file, when I could have put all the config items in an env file, so long as I can have as many of them. 1. The version available on the website, the environment file does not load after you rename it, kindly check that. 2. The code to load the ENV file in the index.php was a very good thing, if we can have it back there it's ok. Or at least the location should be set in the Config/Paths.php, just like the other items in the ROOTPATH..."system", "app" etc.. Thanks for your consideration (05-29-2025, 02:50 PM)chenzen Wrote: In the version 4.4.6...the following code was in the index.php file Because of improvements to the framework. Things change sometimes - in CodeIgniter4 world, especially in the minor versions. (05-29-2025, 02:50 PM)chenzen Wrote: 1. The version available on the website, the environment file does not load after you rename it, kindly check that. The .env is "kind of" hardcoded, so you can't change its name for anything else. (05-29-2025, 02:50 PM)chenzen Wrote: 2. The code to load the ENV file in the index.php was a very good thing, if we can have it back there it's ok. Or at least the location should be set in the Config/Paths.php, just like the other items in the ROOTPATH..."system", "app" etc.. I am not convinced that we should provide a simple way to change the location of the .env file. But if someone sends a PR, we will consider it. That said, if you know exactly what you're doing (and it seems you do), you can create your own version of the Boot.php file inside the app folder. Extend the original class and override the loadDotEnv() method with your custom logic - you can then change both: path and the env file name. Finally, update your index.php to load app/Boot.php instead of the system one, and you're good to go. |
Welcome Guest, Not a member yet? Register Sign In |