Use Case: Config.php File in Magento 2

Use Case: Config.php File in Magento 2

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

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:

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

This is exactly the method where the files become merged. The line

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:

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.

1 comment

Post a new comment