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 (/etc/di.xml)
  • Area-specific (/etc//di.xml)

 

The areas are:

  • adminhtml
  • frontend
  • webapi_rest
  • webapi_soap
  • crontab

 

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




system




moduleConfig


 

  • * 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){}

 

构造参数




adminhtml


xsi:type:

object 类型

{typeName}

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

 

string 类型

{strValue}
{strValue}

boolean 类型

{boolValue}

其它类型值自动转换
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

{numericValue}
  • {numericValue} 整数,小数, 数字字符串

 

init_parameter

{Constant::NAME}

const 常量

{Constant::NAME}

null

array 数组


someVal

 

一个相对完整的例子:





someStringValue

Magento\Some\Type

1

1

Magento\Some\Class::SOME_CONSTANT

Magento\Some\Class::SOME_CONSTANT





Magento\Some\Class::SOME_CONSTANT





ScalarValue

Magento\Some\Class::SOME_CONSTANT




 

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



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

后端的



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


 

重写方法:

 



 

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
---------------------

参数配置继承:




Magento\Core\Model\Url




Magento\Backend\Model\Url


 

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

敏感项或环境项指定




ARGUMENT_VALUE


 

  • 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.

 

 

 

发表回复