Magento – get System configuration fields values – custom module

magento2modulesystem-configsystem-configurationsystem.xml

I have a module for Homepage Banner. I have stated two system config fields inside the module- etc/adminhtml/system.xml.

enter image description here

I need the values of these fields in frontend phtml file "view/frontend/templates/html/banner.phtml".

How will I get these system config fields('enable' and 'banner_content_type') values?

Best Answer

Recommended way

<?php
namespace QaisarSatti\HelloWorld\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    public function getConfig($config_path)
    {
        return $this->scopeConfig->getValue(
            $config_path,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
        );
    }
}

get values in phtml

$helper = $this->helper('QaisarSatti\HelloWorld\Helper\Data');
$helper->getConfig('bannertab/settings/banner_content_type');
$helper->getConfig('bannertab/settings/enable');

Not Recommended way

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$conf = $objectManager->get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue('bannertab/settings/banner_content_type');