سؤال

Poking around Magento 2's core code, it looks like the old class rewrite system has been ripped out and replaced with a new dependency injection system.

Unfortunately, there's no documentation for this new system.

Does anyone have a module configuration sample that would let a user, in Magento 2, "rewrite" the class Mage_Catalog_Model_Product with a new class Packagename_Modulename_Model_Product, using the new dependency injection system?

هل كانت مفيدة؟

المحلول

Di configuration was moved to di.xml files, and format was modified. Now preferences look like this (di.xml):

<config>
    <preference for="{Interface_Or_Class_Name}" type="{Preferred_Class_Name}" />
</config>

نصائح أخرى

Working from the inside out, I was able to get a rewrite working with the following

<global>    
    <di>    
        <preferences>
            <Mage_Catalog_Model_Product>Packagename_Modulename_Model_Product</Mage_Catalog_Model_Product>
        </preferences> 
    </di>
</global>

However, there's a lot more systems code in the dependency injection implementation, so it's not clear if the above will work in the final released version of Magento 2.

You are correct, class rewrites were entirely eliminated and replaced with DI. The following notes are from the section on Application Framework changes:

Magento 1.x — Node: /global//{sub-path which corresponds to factory name + "rewrite" literal}

<global>
...
    <models>
        <core>
            <rewrite>
                <url>My_Module_Model_Url</url>
            </rewrite>
        </core>
    </models>
...
</global>

Magento 2.x — Node: /global/di/aliases

<global>
...
    <di>
        <aliases>
            <Mage_Core_Model_Url>My_Module_Model_Url</Mage_Core_Model_Url>
        </aliases>
    </di>
...
</global>

I haven't tried utilizing this in a module running in Magento 2.0, but it appears that you basically define an alias for the class name instead of using the complicated hierarchy of xml for per-module rewrites.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top