沃梦达 / 编程问答 / php问题 / 正文

Magento - 扩展相同核心类的多个类

Magento - multiple classes extending same core class(Magento - 扩展相同核心类的多个类)

本文介绍了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 使用哪些模块.它们还支持 标签.这个标签允许你说某些模块依赖在另一个模块上,这意味着它们的config.xml将在另一个模块的config.xml之后加载.通过这种方式,您可以控制模块加载的顺序,从而控制哪个合并的重写节点获胜",这反过来又会让您知道哪个类需要成为继承链中的最后一个.

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 - 扩展相同核心类的多个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Magento - 扩展相同核心类的多个类

基础教程推荐