Previously (in Magento 1, that is) we created a module called Setup which contained setting for the site. The upgrade scripts looks something like this:

$installer = $this;
$installer->setConfigData("fastsimpleimport/general/partial_indexing", 1);
$installer->setConfigData("fastsimpleimport/product/disable_preprocess_images", 1);
$installer->setConfigData('general/country/default', 'GB');
$installer->setConfigData('general/locale/firstday', 1); 
$installer->setConfigData('general/locale/timezone', 'Europe/London');
$installer->setConfigData('general/store_information/merchant_country', 'GB');
$installer->setConfigData('design/header/welcome', 'Enter your value');
$installer->setConfigData('design/head/title_suffix', 'Enter your value');
$installer->setConfigData('currency/options/base', 'GBP');
$installer->setConfigData('currency/options/default', 'GBP');
// ...

I can't figure out how to do the above in M2.

In other words: How do I go about setting config data in M2?

  • 2
    why do you need these in the DB? Can't you just add it in the config.xml file? – Marius Feb 23 '16 at 13:12
  • That's just the way things are done. It seems to be the common approach handling settings in module extensions (at least in M1). Also they need to be handled on a version basis so they can be migrated when our module is upgraded. – Martin Wickman Feb 23 '16 at 13:21

This seems to work:

class InstallData implements InstallDataInterface 
    public function __construct(
        LoggerInterface $loggerInterface,
        \Magento\Framework\App\Config\ConfigResource\ConfigInterface  $resourceConfig)
        $this->logger = $loggerInterface;
        $this->resourceConfig = $resourceConfig;

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)


  • 1
    I think it may also work (and be cleaner) to use interface from DI in the constructor for the $resourceConfig object. Like this: \Magento\Framework\App\Config\ConfigResource\ConfigInterface $resourceConfig – Hervé Guétin May 26 '16 at 13:49
  • Hey Martin, can you share some more of your module? I´m struggling in what namespace etc i need to use?! – Klettseb Jul 20 '16 at 15:56
  • why did you add logger also? just a habit or? – OZZIE May 24 '18 at 8:58
  • Definitely not required to add the logger as the InstallDataInterface does not provide a method signature for the constructor, so you are free to use any parameters you like there. As for the module itself: There is nothing else required besides the obligatory registration.php for this script to run during setup after the module has been enabled. – Louis B. Oct 15 '20 at 9:15

The following interface can be used \Magento\Framework\App\Config\Storage\WriterInterface, it has 2 methods:

  • save($path, $value, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = 0)
  • delete($path, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = 0)

It is more high-level compared to \Magento\Config\Model\ResourceModel\Config, and should be used from client code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.