New projects are usually developed on a separate dedicated web-server which settings may differ from those of a production server. So, to avoid mess-ups when trying to back up the production server it is required either to reassign data in the core_config table or to use a special script. The situation goes worse when there is more than one developer involved in the same project, because each developer may use its own custom set of server configurations.
The solution to the problem is the /etc/config.php file which can help us to set up our data configurations. Let’s check the file vendor/magento/framework/Config/File/ConfigFilePool.php and private properties of $applicationConfigFiles and $initialConfigFiles
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
/** * Default files for configuration * * @var array */ private $applicationConfigFiles = [ self::APP_CONFIG => 'config.php', self::APP_ENV => 'env.php', ]; /** * Initial files for configuration * * @var array */ private $initialConfigFiles = [ self::DIST => [ self::APP_CONFIG => 'config.dist.php', self::APP_ENV => 'env.dist.php', ], self::LOCAL => [ self::APP_CONFIG => 'config.local.php', self::APP_ENV => 'env.local.php', ] ]; |
As we see, apart from the main config.php file there are also 2 more files: config.dist.php and config.local.php. As seen from the properties, the files env.dist.php and env.local.php can help us to reassign data. Let’s assume that we need to reassign the configuration for checkout/cart/delete_quote_after
Add the following code to the config.php file:
1 2 3 4 5 6 7 8 9 10 11 |
'system'=> [ 'default' => [ 'checkout' => [ 'cart' => [ 'delete_quote_after' => 31 ] ] ] ] |
Perhaps, it is not the best idea to add new data to the config.php file, instead it is better to use the config.local.php or config.dist.php files for this.
The only thing we need to clarify now is whether it is possible to change the config.php file configuration by creating the same type of settings for config.local.php or config.dist.php files. Let’s check the method \Magento\Framework\App\DeploymentConfig\Reader::loadConfigFile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
/** * @param string $fileKey * @param string $pathConfig * @param bool $ignoreInitialConfigFiles * @return array */ public function loadConfigFile($fileKey, $pathConfig, $ignoreInitialConfigFiles = false) { $result = []; $initialFilePools = $this->configFilePool->getInitialFilePools(); $path = $this->dirList->getPath(DirectoryList::CONFIG); $fileDriver = $this->driverPool->getDriver(DriverPool::FILE); if (!$ignoreInitialConfigFiles) { foreach ($initialFilePools as $initialFiles) { if (isset($initialFiles[$fileKey]) && $fileDriver->isExists($path . '/' . $initialFiles[$fileKey])) { $fileBuffer = include $path . '/' . $initialFiles[$fileKey]; if (is_array($fileBuffer)) { $result = array_replace_recursive($result, $fileBuffer); } } } } if ($fileDriver->isExists($path . '/' . $pathConfig)) { $configResult = include $path . '/' . $pathConfig; if (is_array($configResult)) { $result = array_replace_recursive($result, $configResult); } } return $result; } |
This is exactly the method where the files become merged. The line
1 |
$result = array_replace_recursive($result, $fileBuffer); |
recursively merges env.*.php and config.*.php files. And as we can see the data from env.*.php is replaced by the data from the config.*.php file.
Unfortunately, it is impossible to reassign the data from the config.php with the data from config.*.php files. But generally, we do not need to do that, because config.php contains the information about enabled modules, so, it does not have any information which needs to be modified. The priority of config.local.php is higher than of config.dist.php.
Magento offers convenient ways to reassign file configurations without modifying the database, which lets us arrange flexible development and deploy processes and can also be pretty useful for quality assurance.
UPD. You can back up the current store configurations by the following command:
UPD 2: the file app/etc/config.local.php is not supported by Magento starting from version 2.2.0.
php bin/magento app:config:dump
Consequently, Magento creates config.local.php file and writes the data in it. But please keep in mind that confidential files, such as passwords, will not be included in that file.
Hi, Rafael!
We use it mainly for development tasks. When we take the database from production, config is already set up.
Excellent, thank you for sharing it, could you share how do you use it in your workflow?
Hi,
Thanks for the remark. We will add this information to the article.
Regards.
Sadly from version 2.2.0 file app/etc/config.local.php is not supported:
https://devdocs.magento.com/guides/v2.2/release-notes/backward-incompatible-changes/
Nice and Great Article!