Magento 2 di.xml 用法解析

本文内容来自QQ群大佬陈伟明(一叶知秋)的学习分享

 

di定义了 ,由object manager 注入对象 这个文件相当重要,理解时有难度

参考 https://devdocs.magento.com/guides/v2.4/extension-dev-guide/build/di-xml-file.html

 

主要定义在下面3个场景:

  • Initial (app/etc/di.xml)
  • Global (<moduleDir>/etc/di.xml)
  • Area-specific (<moduleDir>/etc/<area>/di.xml)

 

The areas are:

  • adminhtml
  • frontend
  • webapi_rest
  • webapi_soap
  • crontab

 

这个配置比较核心,可配置很多项
-------------------------
构造方法的行为 注入

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<virtualType name="moduleConfig" type="Magento\Core\Model\Config">
<arguments>
<argument name="type" xsi:type="string">system</argument>
</arguments>
</virtualType>
<type name="Magento\Core\Model\App">
<arguments>
<argument name="config" xsi:type="object">moduleConfig</argument>
</arguments>
</type>
</config>

 

  • * Magento\Core\Model\Config 的构造方法, 参数type 用了system
    public function __construct($type='system'){}
  • * Magento\Core\Model\App 的构造方法,注入了 Magento\Core\Model\Config
    public function __construct(Magento\Core\Model\Config $config){}

 

构造参数

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Core\Model\Session">
<arguments>
<argument name="sessionName" xsi:type="string">adminhtml</argument>
</arguments>
</type>
</config>

xsi:type:

object 类型

<argument xsi:type="object">{typeName}</argument>

<argument xsi:type="object" shared="{shared}">{typeName}</argument>
  • {typeName}可以是 class name, interface name, or virtual type
  • {shared} 定义生命周期 主要是指单例(singleton)和短暂(Transient)的 默认是 singleton, 若是注入上传类,是要求每次实例化 shared="false"

 

string 类型

<argument xsi:type="string">{strValue}</argument>
<argument xsi:type="string" translate="true">{strValue}</argument>

boolean 类型

<argument xsi:type="boolean">{boolValue}</argument>

其它类型值自动转换
NPUT TYPE DATA BOOLEAN VALUE
Boolean true true
Boolean false false
String “true”* true
String “false”* false
String “1” true
String “0” false
Integer 1 true
Integer 0 false

number

<argument xsi:type="number">{numericValue}</argument>
  • {numericValue} 整数,小数, 数字字符串

 

init_parameter

<argument xsi:type="init_parameter">{Constant::NAME}</argument>

const 常量

<argument xsi:type="const">{Constant::NAME}</argument>

null

<argument xsi:type="null"/>

array 数组

<argument xsi:type="array">
<item name="someKey" xsi:type="<type>">someVal</item>
</argument>

 

一个相对完整的例子:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Example\Type">
<arguments>
<!-- Pass simple string -->
<argument name="stringParam" xsi:type="string">someStringValue</argument>
<!-- Pass instance of Magento\Some\Type -->
<argument name="instanceParam" xsi:type="object">Magento\Some\Type</argument>
<!-- Pass true -->
<argument name="boolParam" xsi:type="boolean">1</argument>
<!-- Pass 1 -->
<argument name="intParam" xsi:type="number">1</argument>
<!-- Pass application init argument, named by constant value -->
<argument name="globalInitParam" xsi:type="init_parameter">Magento\Some\Class::SOME_CONSTANT</argument>
<!-- Pass constant value -->
<argument name="constantParam" xsi:type="const">Magento\Some\Class::SOME_CONSTANT</argument>
<!-- Pass null value -->
<argument name="optionalParam" xsi:type="null"/>
<!-- Pass array -->
<argument name="arrayParam" xsi:type="array">
<!-- First element is value of constant -->
<item name="firstElem" xsi:type="const">Magento\Some\Class::SOME_CONSTANT</item>
<!-- Second element is null -->
<item name="secondElem" xsi:type="null"/>
<!-- Third element is a subarray -->
<item name="thirdElem" xsi:type="array">
<!-- Subarray contains scalar value -->
<item name="scalarValue" xsi:type="string">ScalarValue</item>
<!-- and application init argument -->
<item name="globalArgument " xsi:type="init_parameter">Magento\Some\Class::SOME_CONSTANT</item>
</item>
</argument>
</arguments>
</type>
</config>

 

-------------------------------
接口实现 ,方便代码书写只要注入接口约事即可

<config>
<preference for="Magento\Core\Model\UrlInterface" type="Magento\Core\Model\Url" />
</config>

Magento\Core\Model\Url 去实现接口
这样定义后,前端就可以用 Magento\Core\Model\UrlInterface 注入
public function __struction(Magento\Core\Model\UrlInterface $url)

后端的

<config>
<preference for="Magento\Core\Model\UrlInterface" type="Magento\Backend\Model\Url" />
</config>

app/code/Magento/Backend/etc/adminhtml/di.xml 放在 adminhtml/di.xml中


 

重写方法:

 

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Checkout\Block\Onepage\Success" type="ExampleCorp\OverrideExample\Block\Onepage\Success" />
</config>

 

namespace ExampleCorp\OverrideExample\Block\Onepage;

class Success extends \Magento\Checkout\Block\Onepage\Success
{
/**
* Constructor Modification
*
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Checkout\Model\Session $checkoutSession
* @param \Magento\Sales\Model\Order\Config $orderConfig
* @param \Magento\Framework\App\Http\Context $httpContext
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Checkout\Model\Session $checkoutSession,
\Magento\Sales\Model\Order\Config $orderConfig,
\Magento\Framework\App\Http\Context $httpContext,
array $data = []
) {
parent::__construct(
$context,
$checkoutSession,
$orderConfig,
$httpContext,
$data
);
}

/**
* Is order visible
*
* @param Order $order
* @return bool
*/
protected function isVisible(Order $order)
{
# Write your custom logic here.
return !in_array(
$order->getStatus(),
$this->_orderConfig->getInvisibleOnFrontStatuses()
);
}
}

 

重写了 isVisible()方法,重写整个方法不建议,建议采用 plugins 或event
---------------------

参数配置继承:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\View\Element\Context">
<arguments>
<argument name="urlBuilder" xsi:type="object">Magento\Core\Model\Url</argument>
</arguments>
</type>
<type name="Magento\Backend\Block\Context">
<arguments>
<argument name="urlBuilder" xsi:type="object">Magento\Backend\Model\Url</argument>
</arguments>
</type>
</config>

 

Magento\Backend\Block\Context 是 Magento\Framework\View\Element\Context子类 urlBuilder进行的覆盖配置
------------------------

敏感项或环境项指定

<type name="Magento\Config\Model\Config\TypePool">
<arguments>
<argument name="VALUE_TYPE" xsi:type="array">
<item name="CONFIG_PATH" xsi:type="string">ARGUMENT_VALUE</item>
</argument>
</arguments>
</type>

 

  • VALUE_TYPE: Specifies the type of value: either sensitive or environment.
  • CONFIG_PATH: A unique, /-delimited string that identifies this configuration setting.
  • ARGUMENT_VALUE: A value of 1 indicates the CONFIG_PATH value is sensitive or system-specific. The default 0 value indicates it is neither sensitive nor system specific.

 

 

 

【腾讯云】618采购季来袭!爆款云服务器18元起,参与活动享多重好礼
【腾讯云】境外1核2G服务器低至2折,半价续费券限量免费领取!

发表评论