Magento 2 - di.xml 虚拟类型 - 覆盖 class
Magento 2 - di.xml virtual type - overriding a class
我有一个覆盖 ShippingMethodConverter 的模块 class。我在 di.xml 中进行了这样的配置:
<preference for="Magento\Quote\Model\Cart\ShippingMethodConverter"
type="MyNameSpace\MyModule\Quote\Model\Cart\ShippingMethodConverter"/>
<virtualType name="mynamespace_mymodule_quote_mode_cart_shippingmethodconverter" type="\MyNameSpace\MyModule\Quote\Model\Cart\ShippingMethodConverter">
<arguments>
<argument name="shippingMethodConverter" xsi:type="object">\Magento\Quote\Model\Cart\ShippingMethodConverter</argument>
</arguments>
</virtualType>
不幸的是,我收到以下错误:
1 exception(s):
Exception #0 (Exception): Recoverable Error: Argument 2 passed to
Magento\Quote\Model\ShippingMethodManagement::__construct()
must be an instance of Magento\Quote\Model\Cart\ShippingMethodConverter,
instance of MyNameSpace\MyModule\Quote\Model\Cart\ShippingMethodConverter given,
called in /path_to_magento/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php
on line 93 and defined in
/path_to_magento/vendor/magento/module-quote/Model/ShippingMethodManagement.php on line 62
我的class是这样开始的:
namespace MyNameSpace\MyModule\Quote\Model\Cart;
/**
* Quote shipping method data.
*
*/
class ShippingMethodConverter
{
..
根据我对 virtualType 的正确理解,我想说我的 class 应该像给定的参数一样处理,以保证 magento 核心中没有类型解析错误。
您看到的错误是基于此
<preference for="Magento\Quote\Model\Cart\ShippingMethodConverter"
type="MyNameSpace\MyModule\Quote\Model\Cart\ShippingMethodConverter"/>
使用此配置,您告诉 Magento 的自动构造函数依赖注入系统,每当它在构造函数中看到 Magento\Quote\Model\Cart\ShippingMethodConverter
时,它应该实例化一个 MyNameSpace\MyModule\Quote\Model\Cart\ShippingMethodConverter
对象而不是 Magento\Quote\Model\Cart\ShippingMethodConverter
目的。
Magento 正确地执行了此操作,但是由于您的对象未通过构造函数中的类型提示检查,PHP 因错误而放弃。您的 class 需要扩展 Magento\Quote\Model\Cart\ShippingMethodConverter
class(或者如果 Magento\Quote\Model\Cart\ShippingMethodConverter
是一个接口则实现它)
我有一个覆盖 ShippingMethodConverter 的模块 class。我在 di.xml 中进行了这样的配置:
<preference for="Magento\Quote\Model\Cart\ShippingMethodConverter"
type="MyNameSpace\MyModule\Quote\Model\Cart\ShippingMethodConverter"/>
<virtualType name="mynamespace_mymodule_quote_mode_cart_shippingmethodconverter" type="\MyNameSpace\MyModule\Quote\Model\Cart\ShippingMethodConverter">
<arguments>
<argument name="shippingMethodConverter" xsi:type="object">\Magento\Quote\Model\Cart\ShippingMethodConverter</argument>
</arguments>
</virtualType>
不幸的是,我收到以下错误:
1 exception(s):
Exception #0 (Exception): Recoverable Error: Argument 2 passed to
Magento\Quote\Model\ShippingMethodManagement::__construct()
must be an instance of Magento\Quote\Model\Cart\ShippingMethodConverter,
instance of MyNameSpace\MyModule\Quote\Model\Cart\ShippingMethodConverter given,
called in /path_to_magento/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php
on line 93 and defined in
/path_to_magento/vendor/magento/module-quote/Model/ShippingMethodManagement.php on line 62
我的class是这样开始的:
namespace MyNameSpace\MyModule\Quote\Model\Cart;
/**
* Quote shipping method data.
*
*/
class ShippingMethodConverter
{
..
根据我对 virtualType 的正确理解,我想说我的 class 应该像给定的参数一样处理,以保证 magento 核心中没有类型解析错误。
您看到的错误是基于此
<preference for="Magento\Quote\Model\Cart\ShippingMethodConverter"
type="MyNameSpace\MyModule\Quote\Model\Cart\ShippingMethodConverter"/>
使用此配置,您告诉 Magento 的自动构造函数依赖注入系统,每当它在构造函数中看到 Magento\Quote\Model\Cart\ShippingMethodConverter
时,它应该实例化一个 MyNameSpace\MyModule\Quote\Model\Cart\ShippingMethodConverter
对象而不是 Magento\Quote\Model\Cart\ShippingMethodConverter
目的。
Magento 正确地执行了此操作,但是由于您的对象未通过构造函数中的类型提示检查,PHP 因错误而放弃。您的 class 需要扩展 Magento\Quote\Model\Cart\ShippingMethodConverter
class(或者如果 Magento\Quote\Model\Cart\ShippingMethodConverter
是一个接口则实现它)