Let's create the most minimal module there is in Magento. Our module will be called Core and it will belong to the Magelicious vendor. The formula for determining the directory of custom modules is app/code/<VendorName>/<ModuleName>, or in our case <MAGELICIOUS_DIR>/Core.
We start off by creating the <MAGELICIOUS_DIR>/Core/registration.php file with the following content:
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Magelicious_Core',
__DIR__
);
The registration.php file is essentially the entry point of our module. The register method of the Magento\Framework\Component\ComponentRegistrar class provides the ability to statically register components, whereas a component can be more than just a module, as defined via the following constants:
- Magento\Framework\Component\ComponentRegistrar::MODULE
- Magento\Framework\Component\ComponentRegistrar::LIBRARY
- Magento\Framework\Component\ComponentRegistrar::THEME
- Magento\Framework\Component\ComponentRegistrar::LANGUAGE
Next, we will create the <MAGELICIOUS_DIR>/Core/etc/module.xml file with the following content:
<config>
<module name="Magelicious_Core" setup_version="1.0.0">
<sequence>
<module name="Magento_Store"/>
<module name="Magento_Backend"/>
<module name="Magento_Config"/>
</sequence>
</module>
</config>
The name and setup_version attributes of a module element are considered required. The sequence, on the other hand, is optional. We use it to define any potential dependencies around other Magento modules.
Finally, we add composer.json with the following content:
{
"name": "magelicious/module-core",
"description": "The core module",
"require": {
"php": "^7.0.0"
},
"type": "magento2-module",
"version": "1.0.0",
"license": [
"OSL-3.0",
"AFL-3.0"
],
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Magelicious\\Core\\": ""
}
}
}
Magento supports the following composer.json types:
- magento2-module for modules
- magento2-theme for themes
- magento2-language for language packages
- magento2-component for general extensions that do not fit any of the other types
Though composer.json is not required for our custom module to be seen by Magento, it is recommended to add it to any component we are building.
You can trigger module installation by running the php bin/magento module:enable Magelicious_Core command, like so:
$ php bin/magento module:enable Magelicious_Core
The following modules have been enabled:
- Magelicious_Core
To make sure that the enabled modules are properly registered, run 'setup:upgrade'.
Cache cleared successfully.
Generated classes cleared successfully. Please run the 'setup:di:compile' command to generate classes.
Info: Some modules might require static view files to be cleared. To do this, run 'module:enable' with the --clear-static-content option to clear them.
You can run the php bin/magento setup:upgrade command to trigger any install and/or update scripts that need to be triggered:
Cache cleared successfully
File system cleanup:
generated/code/Composer
generated/code/Magento
generated/code/Symfony
Updating modules:
Schema creation/updates:
Module 'Magento_Store':
...
Module 'Magento_CmsUrlRewrite':
Module 'Magelicious_Core':
Module 'Magento_ConfigurableImportExport':
...
Nothing to import.
This finishes our module installation.
Creating the <VendorName>/Core module is often a good practice when working on a project with lots of custom <VendorName> modules. Used carefully, the Core module can provide common bits that are shared across several other modules. The tab element of the system.xml file, which is used to provide a sidebar menu presence under Magento's admin Stores | Settings | Configuration, is a nice example. Similarly, we can use it to provide top-level access resources for other modules to use.
To confirm our module was installed correctly, perform the following:
- Check the <PROJECT_DIR>/app/etc/config.php file for the 'Magelicious_Core' => 1 entry
- Check the setup_module table for the Magelicious_Core 1.0.0 1.0.0 entry
At the moment, our module does absolutely nothing, aside from just sitting there. However, these few simple steps are the basis for us moving forward with Magento development, because the majority of things in Magento are done via a module, alongside other types of components, which we have already mentioned.