Magento 2 示例模块教程
1. 项目的目录结构及介绍
Magento2SampleModule/
├── etc
│ ├── module.xml
│ ├── frontend
│ │ └── routes.xml
├── registration.php
├── Controller
│ └── Index
│ └── Index.php
├── view
│ └── frontend
│ ├── layout
│ │ └── helloworld_index_index.xml
│ ├── templates
│ │ └── helloworld.phtml
目录结构介绍
etc/
: 包含模块的配置文件。module.xml
: 定义模块的基本信息。frontend/routes.xml
: 定义前端路由。
registration.php
: 注册模块。Controller/Index/Index.php
: 控制器文件,处理请求。view/frontend/
: 包含前端视图文件。layout/helloworld_index_index.xml
: 布局文件,定义页面结构。templates/helloworld.phtml
: 模板文件,包含页面内容。
2. 项目的启动文件介绍
registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Tzyganu_Magento2SampleModule',
__DIR__
);
该文件用于注册模块,Tzyganu_Magento2SampleModule
是模块的名称,__DIR__
是模块的目录路径。
3. 项目的配置文件介绍
etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Tzyganu_Magento2SampleModule" setup_version="1.0.0"/>
</config>
该文件定义了模块的名称和版本。
etc/frontend/routes.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route id="helloworld" frontName="helloworld">
<module name="Tzyganu_Magento2SampleModule"/>
</route>
</router>
</config>
该文件定义了前端路由,helloworld
是路由的前缀,Tzyganu_Magento2SampleModule
是模块的名称。
通过以上介绍,您可以了解 Magento 2 示例模块的基本结构和配置。希望这份文档对您有所帮助。