In my Codeigniter 3, I have a simple settings.php file that looks something like this:
<?php
$config["lang1_HTML"] = "sr-Latn-RS";
$config["lang1_HTML_2"] = "sr-Latn";
$config["lang1_HTML_3"] = "";
$config["lang1_code"] = "SRP";
$config["lang1_flag"] = "/images/flags/rs.png";
$config["sr"] = "lang1";
$config["lang3"] = "en";
$config["lang3_HTML"] = "en-RS";
$config["lang3_HTML_2"] = "en";
$config["lang3_HTML_3"] = "";
$config["lang3_code"] = "ENG";
...
Now I want to upgrade this to CI4. Is there any chance to put this file in app\Config without changing it and still be able to access this array?
Or better is it possible to autoload Settings.php and use it like this?
Upgrading from 3.x to 4.x » Upgrade Configuration
Upgrade Guide
You have to change the values in the default CI4 config files according to the changes in the CI3 files. The config names are pretty much the same as in CI3.
If you are using custom config files in your CI3 project you have to create those files as new PHP classes in your CI4 project in app/Config. These classes should be in the
Config
namespace and should extendCodeIgniter\Config\BaseConfig
.Once you have created all custom config classes, you have to copy the variables from the CI3 config into the new CI4 config class as public class properties.
Now, you have to change the config fetching syntax everywhere you fetch config values. The CI3 syntax is something like
$this->config->item('item_name');
. You have to change this intoconfig('MyConfigFile')->item_name;
.
Create a new class app/Config/Settings.php
in the Config
namespace that extends CodeIgniter\Config\BaseConfig
.
Copy the variables from the CI3 config into the new CI4 config class as public class properties. I.e:
<?php
namespace Config;
class Settings extends \CodeIgniter\Config\BaseConfig
{
public string $lang1_HTML = "sr-Latn-RS";
public string $lang1_HTML_2 = "sr-Latn";
public string $lang1_HTML_3 = "";
public string $lang1_code = "SRP";
public string $lang1_flag = "/images/flags/rs.png";
public string $sr = "lang1";
// ...
}
Change the config fetching syntax everywhere you fetch config values.
I.e: from $this->config->item('lang1_HTML');
to config(\Config\Settings::class)->lang1_HTML;
.
CodeIgniter 3.x | CodeIgniter 4.x |
---|---|
1. Loading custom config files. | |
Manual Loading $this->config->load('config_filename'); |
CodeIgniter 4.x will automatically look for the files in all defined namespaces as well as /app/Config/. |
2. Dealing with name collisions. | |
If you need to load multiple config files, normally they will be merged into one master $config array. To avoid collisions you can set the second parameter to TRUE and each config file will be stored in an array index corresponding to the name of the config file. Load config file: $this->config->load('settings', TRUE); Access item: $this->config->item('settings')['lang1_HTML'] |
You don't have to worry about this since all config files reside in their own individual classes. Access item: config(\Config\Settings::class)->lang1_HTML |
3. Fetching Config items. | |
$this->config->item('lang1_HTML'); |
config(\Config\Settings::class)->lang1_HTML |
4. Dynamically setting a config item or changing an existing one. | |
Set item: $this->config->set_item('lang1_HTML', 'sr-Cyrl-ME'); Access set item: $this->config->item('lang1_HTML'); |
Set item: config(\Config\Settings::class)->lang1_HTML = 'sr-Cyrl-ME' Access set item: config(\Config\Settings::class)->lang1_HTML |
5.Auto-loading. | |
Global Auto-loading: Open the autoload.php file, located at application/config/autoload.php, and add your config file as indicated in the file. $autoload['config'] = array('settings'); |
Global Auto-loading: There is no need to configure this since the config(...) helper function returns a shared instance of the particular config class by default. Hence, you can always access your configs using: config('config_class_here')->item_name_here; |