Magento 2.2 introduced a new feature regarding configuration values. Usually, configuration values are stored in the database and defined in the Magento admin area.
However, setting the configuration with code has one huge advantage:
Code can be stored in the versioning system (i.e. Git) and thus be transferred to other instances of the store.
If used correctly, all instances will have the same configuration settings. That is especially important for technical settings like:
- Activating “Flat Catalog”
- Defining payment methods and shipping methods
- Customer settings like which fields should be shown
This could have been done by setup scripts before, but there was no guarantee that the settings wouldn’t be overwritten again. Also, there was the possibility of conflicts when switching branches.
Now, there is a much easier and more reliable method:
Settings can be stored in the configuration files app/etc/env.php and app/etc/config.php
Those files are automatically created during installation of Magento 2 and contain information about the database connection and the installed modules among other things.
For settings like the base url or development settings, it makes sense to store them in a local file so they don’t get overwritten by a database import from another instance. These settings are stored in the app/etc/env.php file which is system specific and won’t be shared among instances.
To add a setting to that file, you would usually use the command line as follows:
bin/magento config:set --lock <path> <value>
The path is the same as you find in the database table core_config_data . An example is:
bin/magento config:set --lock general/store_information/name integer_net
This command automatically adjusts the app/etc/env.php file as follows:
'config' => 1,
'date' => 'Wed, 08 Nov 2017 11:52:21 +0000',
'name' => 'integer_net',
In the Magento admin, it then looks like this:
Do you notice how the field is shown greyed out?
Settings from the configuration files cannot be overwritten.
And that makes perfect sense – no developer will ever have to worry about some crucial configuration settings being overwritten.
Don’t forget the --lock switch – otherwise the setting is just written to the database which has the same effect as if you’d do it in the admin area. You can find more details about the config:set command in the Developer Documentation.
In most cases, configuration settings should be shared among all development, staging and live instances of a shop. To achieve this, you can use the same mechanism as with the local settings described above.
The only difference is that the configuration values will be stored in app/etc/config.php. This file should usually be included in the VCS and thus shared between instances.
To store a config setting in the file via command line, the command would be as follows:
bin/magento config:set --lock-config <path> <value>
The only problem here is that the switch is only implemented in Magento since 2.2.4.. The workaround is to either create that part of the config.php manually, or use the --lock switch and cut/paste the generated code from env.php to config.php.
A Pull Request
To add the --lock-config switch, I have created a Pull Request to the Magento 2 repository. It’s merged into the core code now, and released with Magento 2.2.4.
Wrapping it up
- Configuration settings can be stored in configuration files since Magento 2.2
- Those settings cannot be overwritten in the admin area
- Use app/etc/env.php for system specific configuration
- Use app/etc/config.php for shared configuration
- Always include app/etc/config.php into your VCS (like Git), don’t include app/etc/env.php
- The system specific configuration can be set via command line, the shared configuration too, but only starting from Magento 2.2.4.
- To easier find out the path of a certain configuration setting, you can use the ScopeHint module which shows the path in the admin area.
This is one of two new methods to lock and share your configuration values. The other method allows you to write all your configuration values to the files mentioned before:
I will possibly analyze that method in a future blog post. Meanwhile, you can look it up in the Developer Documentation.
Author: Andreas von Studnitz
Andreas von Studnitz is a Magento developer and CEO of integer_net. His main areas of interest are interface development, backend development, Magento consulting and giving developer trainings. He is a Magento Certified Developer since 2011 and a Magento Certified Solution Specialist since 2014.