我正在开发 Magento 2。
但是找不到在布局 xml 文件中获取 scopeconfig 值的解决方案。
在 magento 1.x 中,使用如下所示。
<block type="cms/block" ...>
<action method="..." ifconfig="config_path/config"></action>
</block>
在magento 2中,如何在布局xml中使用“ifconfig”?
与 magento 1.x 相同。
你可以像下面这样使用。
<block class="Magento\Framework\View\Element\Html\Link\Current" ifconfig="catalog/seo/search_terms" name="search-term-popular-link">
<block class="Ced\Abhinay\Block\Account\Active" ifconfig="ced/account/activation" name="ced_account_activation">
在哪里
Ced = 你的命名空间
Abhinay = 您的模块名称
方法1:使用对象管理器
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$conf = $objectManager
->get('Magento\Framework\App\Config\ScopeConfigInterface')
->getValue('section_id/group_id/field_id');
echo $conf;
?>
方法2:使用助手
在模块的 Helper 文件夹中创建 Data.php 并在其中编写以下代码。
<?php
namespace VendorName\ModuleName\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
);
}
}
?>
您可以通过以下代码在您的 phtml 文件中调用此帮助程序-
<?php
$value=$this->helper('Megha\Menu\Helper\Data')->getConfig('section_id/group_id/field_id');
echo $value;
?>
你可以像下面这样使用。
<block class="Magento\Rss\Block\Feeds" ifconfig="rss/config/active" name="head_rss">
您可以使用以下代码将范围配置值直接获取到 phtml 文件中。
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$conf = $objectManager
->get('Magento\Framework\App\Config\ScopeConfigInterface')
->getValue('group/field/value');
在自定义模块的助手中获取配置值的第二种创建函数的方法
<?php
namespace Vendor\Module\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
);
}
}
然后您可以获取配置值以在任何 phtml 文件中调用此函数。
$this->helper('Vendor\Module\Helper\Data')->getConfig('section/group/field');
注意:请参考以下链接。 https://magento.stackexchange.com/questions/84481/magento-2-how-to-get-the-extensions-configuration-values-in-the-phtml-files强调文本