Magento 2 get system config value
Magento 2 get system config value Magento System.xml configuration values are saved in core_config_data table. There are many ways to get the configuration values in magento 2. Here in this article we are going to explain how you can get the configuration values.
Magento 2 get system config value In Controller | Helper | Model | Block | System Configuration Values | Example
You can get the configuration values in following ways-
\Magento\Framework\App\Config\ScopeConfigInterface class is used in constructor argument to get the configuration values simply as below-
Magento 2 get system config value Example:
<?php namespace Tutorialsplane\HelloWorld\Controller\Index; class Index extends \Magento\Framework\App\Action\Action { protected $resultPageFactory; /** * Constructor * * @param \Magento\Framework\App\Action\Context $context * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory */ public function __construct( \Magento\Framework\App\Action\Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig ) { $this->resultPageFactory = $resultPageFactory; $this->_scopeConfig = $scopeConfig; parent::__construct($context); } /** * Execute view action * * @return \Magento\Framework\Controller\ResultInterface */ public function execute() { return $this->resultPageFactory->create(); } public function getConfigValue(){ $myconfig = $this->_scopeConfig->getValue('section/group/field', \Magento\Store\Model\ScopeInterface::SCOPE_STORE); } } ?> |
In the above example we have created method – getConfigValue() to get the configuration value where you need to pass the section, group, field id and store scope. You can use the same method to get the configuration in Model, Blocks and Helpers as well.
Magento 2 get system config value in Phtml
Using object manage you can get system config value on phtml files. Here is an example of system config value on phtml file.
Magento 2 get system config value Example:
<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $conf = $objectManager->get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue('group/field/value'); ?> |
So using the object manager you can get config values anywhere in your Module.
Advertisements
Add Comment
📖 Read More
Get system config value On Phtml | Block | Controller | Model Anywhere?
Using object manager we can get the configuration values on any phtml Model, block or controller file.