問題描述
我相信我們都遇到過這樣的情況:您有多個擴展,其中一個塊或模型重寫了相同的核心塊/模型.我遇到的問題是:你如何控制 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 - 擴展相同核心類的多個類的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!