如何停止覆盖 magento 模块 config.xml 中的文件?

How to stop overriding the files in module's config.xml in magento?

实际上我的模块覆盖了两个文件。我的模块正在覆盖默认搜索。这是代码:-

<models>
<catalogsearch>
<rewrite>
<indexer_fulltext>WY_SearchIndex_Model_Catalogsearch_Indexer_Fulltext</indexer_fulltext>
<layer>WY_SearchIndex_Model_Catalogsearch_Layer</layer>
</rewrite>
</catalogsearch>
...

现在我想要的是我正在从管理员执行我的模块 enable/disable & 假设它被管理员禁用它不应该覆盖上面的那两个文件 & 因此默认搜索将 运行.

现在我不能把 ifconfig 放在这个 config.xml 对吧?像这样:-

<models>
<catalogsearch ifconfig="searchsphinx/general/enabledornot">

那么可以做什么呢?我现在所做的 - 将条件放入其中一个文件中,即我的模块的 Layer.php 文件,就像这样 -

if(!Mage::getStoreConfig('searchsphinx/general/enabledornot'))
{
//This will call default module's search...there is no method inside the below class
    class WY_SearchIndex_Model_Catalogsearch_Layer extends WY_SearchIndex_Model_Catalogsearch_Layer_Extends
    {
    }
}
else
{
//This will call my module's search
class WY_SearchIndex_Model_Catalogsearch_Layer extends WY_SearchIndex_Model_Catalogsearch_Layer_Extends
    {...}
}

我知道这是非常糟糕的方式,但我在想如果管理员禁用了该模块,那么不应覆盖上述文件。

我该怎么做?有什么建议吗?

谢谢

Magento 管理员禁用模块实际上是禁用模块输出,对于自定义模块,它可能无法像 intended/at 全部那样工作。

如果我的理解是正确的,您不希望此模块在管理面板中运行:

if( Mage::app()->getStore()->isAdmin() )
{
    // Code...

如果你想要一个条件而不是你的方法是正确的 - 你也可以扩展管理界面以提供一种在管理面板中编辑模块配置的方法:

http://inchoo.net/magento/create-configuration-for-your-magento-extension/

有一种方法可以进行有条件的重写,但是我不确定它是否被认为是一种有效的做法。但无论如何,

  1. 从配置中删除重写
  2. 在您的 config.xml 中插入以下内容

    <global>
    ...
        <events>
            <controller_front_init_before>
                <observers>
                    <wy_search_rewrite_classes>
                        <type>model</type>
                        <class>searchsphinx/observer</class>
                        <method>rewriteClasses</method>
                    </wy_search_rewrite_classes>
                </observers>
            </controller_front_init_before>
        </events>
    ...
    </global>
    
  3. 在模型文件夹中创建 Observer.php 文件(如果您没有)

    class WY_SearchIndex_Model_Observer
    {
    
    ...
    
        /**
         * Rewrite necessary classes
         *
         * @param Varien_Event_Observer $observer
         */
        public function rewriteClasses(Varien_Event_Observer $observer)
        {
            $isRewriteEnabled = Mage::getStoreConfig('searchsphinx/general/enabledornot');
            if ($isRewriteEnabled) {
    
                Mage::getConfig()->setNode('global/models/catalogsearch/rewrite/indexer_fulltext',
                    'WY_SearchIndex_Model_Catalogsearch_Indexer_Fulltext');
    
                Mage::getConfig()->setNode('global/models/catalogsearch/rewrite/layer',
                    'WY_SearchIndex_Model_Catalogsearch_Layer');
    
            }
        }
    }