How can I set configuration values in Magento 2?
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)
{
$setup->startSetup();
$this->resourceConfig->saveConfig(
'a/b/c',
'value',
\Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
\Magento\Store\Model\Store::DEFAULT_STORE_ID
);
$setup->endSetup();
}
}
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.