创建一个 Magento2模块

1.在app/code/VendorName/ModuleName/etc/下面创建module.xml 文件

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="VendorName_ModuleName" setup_version="0.0.1" />
</config>

2. 您还需要采取进一步的措施。 除了上述文件外,每个模块还需要一个registration.php文件

#File: app/code/VendorName/ModuleName/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'VendorName_ModuleName',
    __DIR__
);

除了VendorName_ModuleName外,所有模块的这些文件都是相同的。 有几种方法可以检查模块是否正确安装。 一种是使用CLI应用程序的:

php bin/magento module:status

在将新模块安装到系统中之后,您可能会看到以下错误:“Please upgrade your database”,然后您只需运行

php bin/magento setup:upgrade

如果您在列表中看到VendorName_ModuleName,则表示您成功注册了新模块。