2010-11-29 128 views
16

我在模板文件中有一小段代码,如果安装了某个模块,我只想运行它。我找到了下面的代码,可以使用它来查找模块是否处于活动状态,但是我想知道是否安装了模块。Magento - 检查模块是否安装?

$modules = Mage::getConfig()->getNode('modules')->children(); 
$modulesArray = (array)$modules; 

if($modulesArray['Mage_Paypal']->is('active')) { 
    echo "Paypal module is active."; 
} else { 
    echo "Paypal module is not active."; 
} 

我想我也许可以得到已安装的所有模块的名称的列表,然后用

if (stristr($modulelist, 'Name_Extension')) 

显示只有在安装引用的扩展我的代码。

任何任何想法如何做到这一点?或者有更好的解决方案

回答

10

试试这个:

$modules = Mage::getConfig()->getNode('modules')->children(); 
$modulesArray = (array)$modules; 

if(isset($modulesArray['Mage_Paypal'])) { 
    echo "Paypal module exists."; 
} else { 
    echo "Paypal module doesn't exist."; 
} 
+0

看起来这是要做的伎俩。我会先测试一下,所以不会作为答案接受,但绝对看起来不错。 – 2010-11-30 13:33:47

+5

@baoutch是正确的 – WonderLand 2015-03-19 06:54:35

+0

因此,在这个问题中提到的模块有几种状态('已安装','主动'和'已启用')。我真的很喜欢@ baoutch的答案更好,但我不认为这是OP所要求的?无论如何,这是四年前,所以有你去:) – 2015-03-20 19:22:19

1

在您的模块声明中,尝试添加一个depends元素,如this page。 这可能会引发异常,您可以使用try/catch块处理异常。

+0

小比上述更复杂,但可以看到它也工作。感谢发布,因为我相信这将在未来有用... – 2010-11-30 13:41:00

6

另一种方法来发现是否安装了模块,但禁用是:

if (Mage::getStoreConfigFlag('advanced/modules_disable_output/Mage_Paypal')) { 
    echo "Paypal module is installed"; 
} 

编辑
刚才已经实现了一个版本的这一点 - 使用鲜为人知的ifconfig - 仅当另一个模块被禁用时才能显示一个块。例如。

<layout> 
    <default> 
     <reference name="content"> 
      <block ifconfig="advanced/modules_disable_output/Mage_Paypal" type="core/template" template="sorry/this/is/unavailable.phtml" /> 
     </reference> 
    </default> 
</layout> 
+0

干杯这 - 在这种情况下没有用,特别是,但可能会在未来派上用场... – 2010-11-30 13:46:31

2

如果FALSE不存在

80

有一个核心的帮手,你可以检查是否使用这个片段Mage::getConfig()->getNode('modules/Mage_Paypal')回报上安装中存在的模块:

Mage::helper('core')->isModuleEnabled('MyCompany_MyModule'); 

它在Mage_Core_Helper_Abstract

还有一个isModuleOutputEnabled()方法来检查模块的输出是否在系统 - >配置 - >高级 - >禁用模块输出中被禁用。

0

在任何类,模型,控制器甚至PHTML你可以调用,它会工作。

Data.php

class Company_Module_Helper_Data extends Mage_Core_Helper_Abstract 
{ 
    const XML_PATH_GEN_ENABLE = 'Section/Group/Field'; 
    public function isEnable() 
    { 
    return Mage::getStoreConfigFlag(XML_PATH_GEN_ENABLE,Mage::app()->getStore()->getId()); 
    } 
} 

可以调用使用下面的代码Mage::helper('module_name')->isEnable()

您的配置文件是这样

config.xml中

<global> 
    <helpers> 
    <module_name> 
     <class>Company_Module_Helper</class> 
    </module_name> 
    </helpers> 
    // Another Necessary Code for this extension 
</global>