Magento 2 - 获取 scopeconfig 值
Magento 2 - get scopeconfig values
我正在开发 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="Magento\Rss\Block\Feeds" ifconfig="rss/config/active" name="head_rss">
您可以使用以下代码直接将 scop 配置值获取到 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强调文本
你可以简单地使用 like as
<block class="Ced\Abhinay\Block\Account\Active" ifconfig="ced/account/activation" name="ced_account_activation">
在哪里
Ced = Your Namespace
Abhinay = Your Module Name
方法一:使用对象管理器
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$conf = $objectManager
->get('Magento\Framework\App\Config\ScopeConfigInterface')
->getValue('section_id/group_id/field_id');
echo $conf;
?>
方法二:使用助手
在模块的 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;
?>
我正在开发 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="Magento\Rss\Block\Feeds" ifconfig="rss/config/active" name="head_rss">
您可以使用以下代码直接将 scop 配置值获取到 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强调文本
你可以简单地使用 like as
<block class="Ced\Abhinay\Block\Account\Active" ifconfig="ced/account/activation" name="ced_account_activation">
在哪里
Ced = Your Namespace
Abhinay = Your Module Name
方法一:使用对象管理器
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$conf = $objectManager
->get('Magento\Framework\App\Config\ScopeConfigInterface')
->getValue('section_id/group_id/field_id');
echo $conf;
?>
方法二:使用助手
在模块的 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;
?>