Create helper and get system config value Magento 2

Posted at: September 21, 2018 1:36 PM

Create Helper Data.php

Rsgitech\News\Helper\Data.php


<?php
namespace Rsgitech\News\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Store\Model\ScopeInterface;

class Data extends AbstractHelper 
{
	const XML_PATH_RSGITECH_NEWS = 'rsgitech_news/';

	public function getConfigValue($field, $storeCode = null)
	{
		return $this->scopeConfig->getValue($field, ScopeInterface::SCOPE_STORE, $storeCode);
	}

	public function getGeneralConfig($fieldid, $storeCode = null)
	{
		return $this->getConfigValue(self::XML_PATH_RSGITECH_NEWS.'general/'.$fieldid, $storeCode);
	}

	public function getStorefrontConfig($fieldid, $storeCode = null)
	{
		return $this->getConfigValue(self::XML_PATH_RSGITECH_NEWS.'storefront/'.$fieldid, $storeCode);
	}
}
?>

Controller Action Index.php

Rsgitech\News\Controller\Adminhtml\AllNews\Index.php

You can get system configuration value


<?php
namespace Rsgitech\News\Controller\Adminhtml\AllNews;

class Index extends \Magento\Backend\App\Action
{
	protected $resultPageFactory;
	
	protected $helperData;
	
	public function __construct(
		\Magento\Backend\App\Action\Context $context,
		\Magento\Framework\View\Result\PageFactory $resultPageFactory,
		\Rsgitech\News\Helper\Data $helperData
	) {
		parent::__construct($context);
		$this->resultPageFactory = $resultPageFactory;
		$this->helperData = $helperData;
	}

	public function execute()
	{
		echo $this->helperData->getStorefrontConfig('news_link');
		exit();
		
		$resultPage = $this->resultPageFactory->create();
		
		return $resultPage;
	}
}
?>

Then run below commands


php bin/magento setup:upgrade
php bin/magetno setup:static-content:deploy
php bin/magento cache:flush

Complete!!

Download source code: Download

This lesson also available on YouTube
Create Helper Helper Get System Config Value System Configuration Magento 2

Please leave comments

0 Comments