Magento 2 - 获得scopeconfig值

时间:2015-09-15 09:40:16

标签: php magento magento2

我正在使用Magento 2。

但无法找到在布局xml文件中获取scopeconfig值的解决方案。

在magento 1.x中,使用如下所示。

<block type="cms/block" ...>
    <action method="..." ifconfig="config_path/config"></action>
</block>

在magento 2中,如何使用&#34; ifconfig&#34;在布局xml?

5 个答案:

答案 0 :(得分:2)

与magento 1.x相同。

您可以使用以下内容。

{{1}}

答案 1 :(得分:1)

您可以简单地使用as

<block class="Ced\Abhinay\Block\Account\Active" ifconfig="ced/account/activation" name="ced_account_activation">

哪里

  

Ced =您的命名空间

     

Abhinay =您的模块名称

答案 2 :(得分:0)

您可以使用以下内容。

<block class="Magento\Rss\Block\Feeds" ifconfig="rss/config/active" name="head_rss">

答案 3 :(得分:0)

您可以使用以下代码将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 强调文字

答案 4 :(得分:0)

方法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;
?>