magento2配置文件管理之模块配置文件说明

7天成为Magento系统架构师,现在开始学习Magento全栈开发!

《Magento2.X企业级开发实战》

模块配置文件概览
Magento 早期版本中使用的 config.xml 配置文件的职责现在分为几个文件,位于不同的模块目录中。 Magento 的多个配置文件仅在模块请求特定配置类型时按需加载。

您可以使用这些文件(也称为配置类型)来自定义模块行为的特定方面。

多个模块可以声明影响相同配置类型(例如事件)的配置文件,并且这些多个配置文件被合并。

以下是本主题中使用的常用术语:

配置对象——负责定义和验证配置类型的 Magento 库或类。例如,config.xml 的配置对象是 Magento\Framework\App\Config。

配置阶段——阶段被定义为主要、全局和区域。每个阶段确定何时加载配置类型并与同名配置类型合并。例如,module.xml 文件与其他 module.xml 文件合并。有关更多信息,请参阅配置加载和合并。

配置范围——作为配置阶段的补充,范围定义了配置类型模型。例如,adminhtml 是一个区域作用域,在阶段加载其他模块的 adminhtml 配置。有关更多信息,请参阅模块和区域。

配置加载和合并
本节讨论如何加载和合并配置文件。

Magento 如何加载配置文件
Magento 按以下顺序加载配置文件(所有路径都相对于您的 Magento 安装目录):

主要配置 (app/etc/di.xml)。该文件用于引导 Magento。
来自模块的全局配置(<您的组件基础目录>//-/etc/.xml)。从所有模块中收集某些配置文件并将它们合并在一起。 来自模块的区域特定配置(<您的组件基础目录>/<供应商名称>/<组件类型>-<组件名称>/etc/<区域>/.xml)。从所有模块中收集配置文件并将它们合并到全局配置中。某些特定于区域的配置可以覆盖或扩展全局配置。
在哪里

是您的组件所在的基本目录。典型值为 app/code 或 vendor 相对于 Magento 安装目录。
是组件的供应商名称;例如,Magento 的供应商名称是 magento。
是以下之一:

模块-:扩展或模块。
主题-:主题。
语言-:语言包。
目前,主题位于 /app/design/frontend 或 /app/design/adminhtml 下。
:在 composer.json 中定义的组件名称。
配置文件合并
配置文件中的节点根据其完全限定的 XPath 进行合并,该 XPath 在 $idAttributes 数组中定义了一个特殊属性,声明为它的标识符。对于嵌套在同一父节点下的所有节点,此标识符必须是唯一的。

Magento 的合并算法如下:

如果节点标识符相等(或者如果没有定义标识符),则节点中的所有基础内容(属性、子节点和标量内容)都将被覆盖。
如果节点标识符不相等,则该节点是父节点的新子节点。
如果原始文档有多个节点具有相同的标识符,则会因为标识符无法区分而引发错误。
配置文件合并后,生成的文档包含原始文件中的所有节点。

请注意,您可以使用 \Magento\Framework\Config\Reader\Filesystem 类来调试和理解配置文件加载器和合并配置过程背后的逻辑。
配置类型、对象和接口
以下部分提供了有关配置类型、它们对应的配置对象以及可用于处理这些对象的接口的信息:

配置类型和对象
配置接口
配置类型和对象
下表显示了每种配置类型以及与之相关的 Magento 配置对象。

Configuration fileDescriptionStageConfiguration object
address_formats.xmlAddress format declarationprimary, global\Magento\Customer\Model\Address\Config
acl.xmlAccess Control Listglobal\Magento\Framework\Acl\AclResource\Provider
analytics.xmlAdvanced reportingprimary, global\Magento\Analytics\Model\Config\Reader
cache.xmlCache type declarationprimary, global\Magento\Framework\Cache\Config\Data
catalog_attributes.xmlCatalog attributes configurationglobal\Magento\Catalog\Model\Attribute\Config\Data
config.php and env.phpDeployment configurationThese files are readable/writeable by the internal config processor.Has no object, cannot be customized
config.xmlSystem configurationprimary, global\Magento\Framework\App\Config
communication.xmlDefines aspects of the message queue systemglobal\Magento\WebapiAsync\Code\Generator\Config\RemoteServiceReader\Communication
crontab.xmlConfigures cron groupsglobal\Magento\Cron\Model\Config\Data
cron_groups.xmlSpecifies cron group optionsglobal\Magento\Cron\Model\Groups\Config\Data
db_schema.xmlDeclarative schemaglobalMagento\Framework\Setup\Declaration\Schema
di.xmlDependency injection configurationprimary, global, area\Magento\Framework\ObjectManager\Config
eav_attributes.xmlProvides EAV attributes configurationglobal\Magento\Eav\Model\Entity\Attribute\Config
email_templates.xmlEmail templates configurationglobal\Magento\Email\Model\Template\Config\Data
esconfig.xmlElasticsearch locale stopwords configglobal\Magento\Elasticsearch\Model\Adapter\Index\Config\EsConfig
events.xmlEvent/observer configurationglobal, area\Magento\Framework\Event
export.xmlExport entity configurationglobal\Magento\ImportExport\Model\Export\Config
extension_attributes.xmlExtension attributesglobal\Magento\Framework\Api\ExtensionAttribute\Config
fieldset.xmlDefines fieldsetsglobal\Magento\Framework\DataObject\Copy\Config\Reader
indexer.xmlDeclares indexersglobal\Magento\Framework\Indexer\Config\Reader
import.xmlDeclares import entitiesglobal\Magento\ImportExport\Model\Import\Config
menu.xmlDefines menu items for admin paneladminhtml\Magento\Backend\Model\Menu\Config\Reader
module.xmlDefines module config data and soft dependencyprimary, global\Magento\Framework\Module\ModuleList\Loader
mview.xmlMView configurationprimary, global\Magento\Framework\Mview\Config\Data
payment.xmlPayment module configurationprimary, global\Magento\Payment\Model\Config
persistent.xmlMagento_Persistent configuration fileglobal\Magento\Persistent\Helper\Data
pdf.xmlPDF settingsglobal\Magento\Sales\Model\Order\Pdf\Config\Reader
product_options.xmlProvides product options configurationglobal\Magento\Catalog\Model\ProductOptions\Config
product_types.xmlDefines product typeglobal\Magento\Catalog\Model\ProductTypes\Config
queue_consumer.xmlDefines the relationship between an existing queue and its consumerglobal\Magento\Framework\MessageQueue\Consumer\Config\Xml\Reader
queue_publisher.xmlDefines the exchange where a topic is published.global\Magento\WebapiAsync\Code\Generator\Config\RemoteServiceReader\Publisher
queue_topology.xmlDefines the message routing rules, declares queues and exchangesglobal\Magento\Framework\MessageQueue\Topology\Config\Xml\Reader
reports.xmlAdvanced reportsglobal\Magento\Analytics\ReportXml\Config
resources.xmlDefines module resourceglobal\Magento\Framework\App\ResourceConnection\Config\Reader
routes.xmlRoute configurationareaMagento\Framework\App\Route\Config
sales.xmlDefines sales total configurationglobal\Magento\Sales\Model\Config\Data
search_engine.xmlProvides search engine configurationglobalMagento\Search\Model\SearchEngine\Config
search_request.xmlDefines catalog search configurationglobal\Magento\Framework\Search\Request\Config
sections.xmlDefines actions that trigger cache invalidation for private content blocksfrontendSectionInvalidationConfigReader
system.xmlDefines options for system configuration pageadminhtml\Magento\Framework\App\Config
validation.xmlModule validation configuration fileglobal\Magento\Framework\Validator\Factory
view.xmlDefines Vendor_Module view config valuesglobal\Magento\Framework\View\Config
webapi.xmlConfigures a web APIglobal\Magento\Webapi\Model\Config
webapi_async.xmlDefines REST custom routesglobal\Magento\WebapiAsync\Model\ServiceConfig
widget.xmlDefines widgetsglobal\Magento\Widget\Model\Config\Reader
zip_codes.xmlDefines zip code format for each countryglobal\Magento\Directory\Model\Country\Postcode\Config\Data

配置接口
您可以使用 Magento\Framework\Config 下的接口与配置文件进行交互。

如果您创建新的配置类型,也可以使用这些接口。

Magento\Framework\Config 提供以下接口:

Framework\Config\ConverterInterface,它将 XML 转换为配置的内存中数组表示。
Framework\Config\DataInterface,检索指定范围内的配置数据。
Framework\Config\FileResolverInterface,它标识了 Magento\Framework\Config\ReaderInterface 要读取的文件的位置。
Framework\Config\ReaderInterface,它从存储中读取配置数据,并选择从中读取的存储。
即文件系统、数据库、其他存储根据合并规则合并配置文件,并用验证模式对配置文件进行验证。

Framework\Config\SchemaLocatorInterface,用于定位 XSD 架构。
Framework\Config\ScopeListInterface,它返回范围列表。
Framework\Config\ValidationStateInterface,它检索验证状态。

如无特殊说明或标注,任何个人或组织,复制、转载、采集本站内容请注明:
本文来源于:【Magento中文网】,并添加本文地址链接。
如未按上述操作复制或转载,本站有权追究法律责任。
若本站内容侵犯了原著者的合法权益,可联系我们进行处理。