Magento-扩展同一核心类的多个类 [英] Magento - multiple classes extending same core class

查看:46
本文介绍了Magento-扩展同一核心类的多个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我敢肯定,我们所有人都遇到了这样的情况,即您有一个具有多个块或模型的扩展,这些块或模型重写了相同的核心块/模型.我遇到的问题是:您如何控制Magento看到这些类的顺序?

I'm sure we've all run into a situation where you have multiple extensions with a block or model that rewrites the same core block/model. The problem I've run into is this: How do you control the order in which Magento sees these classes?

例如,假设我们有2个扩展,具有以下2个类:

For example, let's say we have 2 extensions with the following 2 classes:

config.xml

<catalog>
    <rewrite>
        <product_view>My_ClassA_Block_Catalog_Product_View</product_view>
    </rewrite>
</catalog>

My/ClassA/Block/Catalog/Product/View.php

class My_ClassA_Block_Catalog_Product_View extends Mage_Catalog_Block_Product_View {}

B级

<catalog>
    <rewrite>
        <product_view>My_ClassB_Block_Catalog_Product_View</product_view>
    </rewrite>
</catalog>

My/ClassB/Block/Catalog/Product/View.php

class My_ClassB_Block_Catalog_Product_View extends Mage_Catalog_Block_Product_View {}

-

推荐的解决方案是更改其中一个,以便它们扩展另一个并将它们链接在一起(class A extends B {}class B extends C {}等):

My/ClassA/Block/Catalog/Product/View.php

class My_ClassA_Block_Catalog_Product_View extends My_ClassB_Block_Catalog_Product_View {}

My/ClassB/Block/Catalog/Product/View.php

class My_ClassB_Block_Catalog_Product_View extends Mage_Catalog_Block_Product_View {}

-

我遇到的问题是Magento不一定会这样看.我不知道它是按字母顺序排列还是随机排列,但有时这可行,有时却不可行.在某些情况下,Magento优先考虑 ClassB ,所有对createBlock('catalog/product_view')的调用都会创建 ClassB 的实例,从而完全绕过 ClassA 中的任何代码.

--

The problem I've run into is that Magento doesn't necessarily see it that way. I don't know if it's alphabetical or somewhat random, but sometimes this works and sometimes it doesn't. In some cases, Magento gives priority to ClassB and all calls to createBlock('catalog/product_view') create an instance of ClassB, completely bypassing any code in ClassA.

所以我的问题是:当2个不同的扩展名都重写核心catalog_product_view类时,如何控制createBlock('catalog/product_view')实例化哪个类?

So my question is this: How do I control which class gets instantiated by createBlock('catalog/product_view') when 2 different extensions both rewrite the core catalog_product_view class?

推荐答案

当Magento提取用于特定块的类时,它将在合并的config.xml树中查找单个节点,

When Magento fetches the class to use for a particular block, it looks inside the merged config.xml tree for a single node at

catalog/rewrite/product_view

多次重写的问题是,由于Magento加载模块的XML,将其与配置树合并,然后加载另一个,因此只能存在一个节点. >模型.这意味着您只能将一个类别名解析为一个类名称.

The problem with multiple rewrites is, only one node can be there due to the way Magento loads a module's XML, merges it with the config tree, and then loads another model. This means you can only ever have one class alias resolve to one class name.

这就是

app/etc/modules/*.xml

发挥作用.这些文件告诉Magento使用哪个模块.他们还支持<depends>标记.这个标签允许您说某些模块依赖依赖于另一个模块,这意味着它们的config.xml将在另一个模块的config.xml之后加载.这样,您可以控制模块的加载顺序,从而控制合并的重写节点"wins",从而使您知道继承链中的哪个类是最终的.

come into play. These files tell Magento which modules to use. They also have support for a <depends> tag. This tag allows you to say certain modules depend on another module, which means their config.xml will be loaded after another module's config.xml. In this way, you can control which order the modules are loaded in, and therefore control which merged rewrite node "wins", which in turn will allow you to know which class needs to be the final in your inheritance chain.

这篇关于Magento-扩展同一核心类的多个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆