Skip to content
intermediate Phase 31 · Admin & System XML

config.xml — Default Configuration Values

Default config values, config types, config scopes, and how config.xml interacts with core_config_data.

45m
0 problems
Topic Progress 0%

config.xml Structure

config.xml defines default values for module configuration settings.

Basic config.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
        <vendor_module>
            <general>
                <enabled>1</enabled>
                <title>My Module</title>
                <timeout>30</timeout>
            </general>
            <api>
                <key></key>
                <secret></secret>
            </api>
        </vendor_module>
    </default>
</config>

Config path mapping:

vendor_module/general/enabled → 1
vendor_module/general/title → My Module
vendor_module/general/timeout → 30
vendor_module/api/key → (empty)

Accessing config in PHP:

// Using ScopeConfigInterface
$enabled = $this->scopeConfig->getValue('vendor_module/general/enabled');
$title = $this->scopeConfig->getValue('vendor_module/general/title');
$timeout = (int)$this->scopeConfig->getValue('vendor_module/general/timeout');

// With scope
$value = $this->scopeConfig->getValue(
    'vendor_module/general/title',
    \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
    'store_code'
);

config.xml vs core_config_data:

config.xml → Default values (code)
core_config_data → Overridden values (database)

Priority: core_config_data > config.xml

Config Scopes

Configuration values can vary by scope: default, website, and store.

Scope hierarchy:

Default (global)
└── Website
    └── Store View

Scoped config.xml:

<!-- Default scope -->
<default>
    <vendor_module>
        <general>
            <title>Default Title</title>
        </general>
    </vendor_module>
</default>

Website scope override:

// Store > Config > Configuration > Vendor Module > General > Title
// Set for specific website

Store scope override:

// Store > Config > Configuration > Vendor Module > General > Title
// Set for specific store view

Scope resolution:

// Magento checks in order:
// 1. Store view value
// 2. Website value
// 3. Default value

$value = $this->scopeConfig->getValue(
    'vendor_module/general/title',
    ScopeInterface::SCOPE_STORE,
    'german_store' // Check this store first
);

// If not found at store level → check website → check default

Using config in templates:

// In Block class
public function isEnabled(): bool
{
    return (bool)$this->_scopeConfig->getValue(
        'vendor_module/general/enabled',
        ScopeInterface::SCOPE_STORE
    );
}

Config in system.xml:

<!-- Scope visibility controlled by showIn attributes -->
<field id="title" type="text" showInDefault="1" showInWebsite="1" showInStore="1">
    <label>Title</label>
</field>

When to use each scope:

  • Default: Global settings (API keys, feature toggles)
  • Website: Per-website settings (payment methods, shipping)
  • Store: Per-store-view settings (labels, translations)

Config Management via CLI

Magento provides CLI commands for managing configuration values.

View config values:

# Show specific config value
php bin/magento config:show vendor_module/general/enabled

# Show all config for a section
php bin/magento config:show vendor_module/

# Show all config
php bin/magento config:show

Set config values:

# Set value for default scope
php bin/magento config:set vendor_module/general/title "New Title"

# Set value for specific scope
php bin/magento config:set vendor_module/general/title "Website Title" --scope=website --scope-code=default

# Set encrypted value
php bin/magento config:set vendor_module/api/key "abc123" --encrypted

Delete config values:

# Delete override (revert to default)
php bin/magento config:delete vendor_module/general/title

# Delete for specific scope
php bin/magento config:delete vendor_module/general/title --scope=store --scope-code=german

Database direct access:

-- View config values
SELECT * FROM core_config_data WHERE path LIKE 'vendor_module/%';

-- Insert/update config
INSERT INTO core_config_data (scope, scope_id, path, value)
VALUES ('default', 0, 'vendor_module/general/title', 'My Title')
ON DUPLICATE KEY UPDATE value = 'My Title';

-- Website scope
INSERT INTO core_config_data (scope, scope_id, path, value)
VALUES ('website', 1, 'vendor_module/general/title', 'Website Title');

Config cache:

# Clear config cache after changes
php bin/magento cache:clean config

# Flush all caches
php bin/magento cache:flush

Config.xml Best Practices

Best practices for managing configuration in Magento.

Default values pattern:

<!-- config.xml: Set sensible defaults -->
<default>
    <vendor_module>
        <general>
            <enabled>1</enabled>
            <title>Default Title</title>
            <items_per_page>20</items_per_page>
        </general>
    </vendor_module>
</default>

Avoid hardcoded defaults:

// BAD: Hardcoded default
$title = $this->scopeConfig->getValue('vendor_module/general/title') ?? 'Default';

// GOOD: Default in config.xml
$title = $this->scopeConfig->getValue('vendor_module/general/title');
// config.xml provides the default

Config validation:

public function getValue(string $path): ?string
{
    $value = $this->scopeConfig->getValue($path);
    
    if ($value === null || $value === '') {
        throw new \Magento\Framework\Exception\LocalizedException(
            __('Configuration %1 is not set', $path)
        );
    }
    
    return $value;
}

Sensitive data handling:

<!-- Use type="obscure" for sensitive config -->
<field id="api_secret" type="obscure" showInDefault="1">
    <label>API Secret</label>
    <backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
</field>

Config export/import:

# Export config
curl -X POST "https://example.com/rest/V1/config" -H "Authorization: Bearer TOKEN"

# Import config (typically via API)
php bin/magento app:config:import

Quiz

1. Which takes precedence: config.xml or core_config_data?

Question 1 options

2. What is the scope resolution order for config values?

Question 2 options

3. How do you set a config value via CLI?

Question 3 options

4. Where does config.xml store its values?

Question 4 options

Flashcards

Question

What does config.xml define?

Answer

Default configuration values for modules

Question

What takes precedence over config.xml?

Answer

core_config_data (database values)

Question

What is the scope hierarchy?

Answer

Default → Website → Store (Store checked first)

Question

How do you view config values via CLI?

Answer

php bin/magento config:show path

Question

How do you set encrypted config values?

Answer

php bin/magento config:set path value --encrypted

Revision Notes

Key Takeaways

  • 1. config.xml defines default values; core_config_data overrides them
  • 2. Config scope order: Store → Website → Default
  • 3. CLI commands: config:show, config:set, config:delete
  • 4. Sensitive data uses type='obscure' with Encrypted backend model
  • 5. Config changes require cache flush
  • 6. Access config via ScopeConfigInterface::getValue()

Interview Tips

  • Explain config.xml vs core_config_data precedence
  • Describe scope resolution for multi-store setups
  • Know CLI commands for config management
  • Discuss how to handle sensitive configuration

Cheat Sheet

config.xml Cheat Sheet

Purpose: Default config values
File: Vendor/Module/etc/config.xml

Structure:

<default>
    <vendor_module>
        <group>
            <field>value</field>
        </group>
    </vendor_module>
</default>

Precedence:

  1. core_config_data (DB)
  2. config.xml (code)

Scope check order:
Store → Website → Default

CLI:

  • config:show path
  • config:set path value
  • config:delete path
  • cache:flush (after changes)

Access in PHP:

$this->scopeConfig->getValue('path/to/config')