To create basic Magento 2 module you need only 2 files: module.xml and registration.php.
1. Firstly, create the module's folder:
app/code/<VendorName>/<ModuleName>/
and the folder that will contain the module configuration files:
app/code/<VendorName>/<ModuleName>/etc/
If the app/code folder is missing from your Magento 2 installation, please create one.
2. Place the module.xml file with the following contents in the app/code/<VendorName>/<ModuleName>/etc/ folder
<?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="VendorName_ModuleName" setup_version="2.0.0">
<sequence>
<module name="Magento_Cms"/>
<module name="Magento_Catalog"/>
</sequence>
</module>
</config>
sequence is not a required element and is intended to define the modules which your module depends on. If the dependencies are unknown, they can be specified later.
setup_version — defines the current version of the module.
3. Create a registration.php file and place it in the app/code/<VendorName>/<ModuleName>/ folder.
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'VendorName_ModuleName',
__DIR__
);
In the examples, we will create a module Magefan_Faq for Frequently Asked Question (FAQ).
The resulting structure of the module will look like this:
├── app │ └── code │ └── Magefan // назва розробника - VendorName
│ └── Faq // назва модуля - ModuleName
│ ├── etc
│ │ └──module.xml
│ │
│ └── registration.php
│
On GitHub you can view the files of the Magefan_Faq module.
After creating these 2 files, register the module in the system. Open CLI (terminal, command line), then go to the Magento 2 root folder and run the following command:
php bin/magento setup:upgrade
It checks for new and updated system components (modules, themes, language packs), installs and updates them.
You can check if the module is installed and enabled in this file
app/etc/config.php
there must be the following string in it
'VendorName_ModuleName' => 1
We have created the simplest module for Magento 2. It is a separate unit, though it does not have any functionality yet.
Read more on how to create your own controller and display "Hello World".