How to get data from Magento System Configuration

How to get data from Magento System Configuration

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to Get Data from Magento System Configuration

When building a custom module in Magento, you often encounter configuration fields defined in the admin/system/configuration section. These settings allow store administrators to tweak behavior without touching core code. However, simply defining these fields isn't enough; your frontend logic needs a reliable way to read them. The mechanism for this varies slightly depending on which version of Magento you are working with, specifically between Magento 1 (CE/EE) and Magento 2, where the architecture shifted significantly toward dependency injection and service contracts.

Magento 1: Event Observers and Block Classes

In Magento 1, the most straightforward approach to retrieving system configuration values is through the Mage_Core_Block_Template class or by creating a specific block. The core model provides a helper method that abstracts the complexity of database lookups behind a simple string key.

You can access these values within your layout XML using the config directive in your block definition, but for programmatic access inside PHP logic, you typically instantiate the configuration object or use the static helper provided by the core module:

use Mage_Core_Model_Config;

$config = Mage::getModel('core/config');
$value = $config->getValue('your_module/section/frontend_setting');

Alternatively, if you are working within a block class, Magento provides a built-in method called __construct where you can resolve values. However, the most common pattern involves using the static helper directly:

$settings = Mage::getStoreConfig('your_module/section/frontend_setting');

This method looks for the path defined in your adminhtml/system_config.xml. If a value is set to "No Value" (empty string) by the admin, this will return an empty string. It is crucial to handle this case explicitly in your logic, as treating an empty string as null can lead to unexpected behavior in calculations or conditionals later in the request lifecycle.

Magento 2: The Configuration Interface and Dependency Injection

The approach changes entirely in Magento 2. The architecture moved away from static method calls on global objects to a strict dependency injection model. To access system configuration, you must inject an instance of \Magento\Framework\App\Config\ScopeConfigInterface.

This interface allows you to read values scoped to the Store View, Website, or Default Store level. You would typically define this in your constructor:

use Magento\Framework\App\Config\ScopeConfigInterface;

class MyController extends \Magento\Framework\App\Action\HttpGetAction
{
    protected $scopeConfig;

    public function __construct(
        ScopeConfigInterface $scopeConfig,
        // ... other dependencies
    ) {
        $this->scopeConfig = $scopeConfig;
    }
}

Once injected, retrieving a value requires specifying the scope type (e.g., Store, Website, or DefaultScope) and the configuration path. The path corresponds to the XML definition in your module's etc/adminhtml/system.xml.

$setting = $this->scopeConfig->getValue(
    'your_module/section/frontend_setting',
    ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
    null // Default value to return if not found
);

The second parameter dictates the scope level. If your configuration is set at the Website level but you query with Store scope without a specific store ID, Magento will fall back to the default value or throw an exception depending on how strictly the code is written. Always ensure the scope matches where the data was actually saved in the admin panel.

Handling Defaults and Scope Logic

A common pitfall when implementing configuration retrieval is ignoring the third parameter of getValue, which represents the default value. If a configuration path does not exist or has been explicitly set to empty, passing null as the default might break your code if you assume a boolean or integer type.

Consider this scenario:

$isEnabled = $this->scopeConfig->getValue(
    'your_module/section/is_active', 
    ScopeConfigInterface::SCOPE_TYPE_DEFAULT, 
    false // Explicitly set a safe default
);
if ($isEnabled) {
    // Perform action
}

By providing an explicit default, you prevent runtime errors when the configuration path is missing. This is particularly important in Magento 2, where strict type hinting and static analysis tools often flag potential issues with undefined variables or null returns.

If you are dealing with complex nested paths or need to validate that a setting exists before reading it, checking the result against your defined default provides a robust safeguard. Always remember that configuration data is stored in the core_config_data database table, and while Magento abstracts this, understanding the underlying scope hierarchy (Store > Website > Default) ensures you are fetching the intended value rather than a global fallback.

Our network