Cannot read configuration defined in config.xml file inside frontend area

I'm pretty new to Magento 2, so most probably I'm missing something simple here.

I've purchased a certification prep course. One of the first examples was about config merging and overriding. There were two modules: one with a config.xml file in its /etc folder (let's call it module A), containing this code:

<?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>
        <example>
            <values>
                <shipping_method>fedex</shipping_method>
                <payment_method>paypal</payment_method>
            </values>
        </example>
    </default>
</config>

And then another one, module B, with another config.xml file placed in etc/frontend with this code:

<?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>
        <example>
            <values>
                <shipping_method>ups</shipping_method>
            </values>
        </example>
    </default>
</config>

Due to the sequence defined in module.xml, module A loads after module B. When I inspected the values for those configuration nodes, everything seemed to work fine.

But then I tried to invert the order, I realized Magento wasn't loading the config.xml file in module B, so overriding wasn't actually happening.

To double-check that, I defined a new module, and inside its etc/frontend folder I created a config.xml file with this content:

<?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>
        <example>
            <values>
                <shipping_method>fedex</shipping_method>
                <payment_method>paypal</payment_method>
            </values>
        </example>
    </default>
</config>

I've also created an Action class with this content:

class Index extends \Magento\Framework\App\Action\Action
{
    /** @var RawFactory */
    private $rawFactory;

    /** @var ScopeConfigInterface */
    private $scopeConfig;

    public function __construct(
        Context $context,
        RawFactory $rawFactory,
        ScopeConfigInterface $scopeConfig
    ) {
        $this->rawFactory = $rawFactory;
        $this->scopeConfig = $scopeConfig;
        parent::__construct($context);
    }

    public function execute()
    {
        $shippingMethod = $this->scopeConfig->getValue('example/values/shipping_method');
        $paymentMethod = $this->scopeConfig->getValue('example/values/payment_method');

        $output = $this->rawFactory->create();
        $output->setContents(implode("<br/>", [
            'Shipping method: ' . $shippingMethod,
            'Payment method: ' . $paymentMethod
        ]));

        return $output;
    }
}

The issue is $shippingMethod and $paymentMethod are both null, unless I move the config.xml file to etc root folder. I've run this on that controller method:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$objectManager->get('Magento\Framework\App\State')->getAreaCode();

And the result is frontend.

What am I missing? I should be able to get the values from config.xml when it's located in the frontend area, should I? I've disabled the cache, cleared opcache anytime I made a change, run magento setup:di:compile, and it always returns null for the configuration values when config.xml is inside etc/frontend.

In short, why Magento is not able to load config.xml from etc/frontend?

Answers 3

Related Questions