If you have been looking at Magento 2 closely you are probably wondering where the etc/modules folder went and how to declare your custom module. In the previous version of Magento, we would simply let Magento know that our module exists by adding an .xml file directly in the modules folder. This in turn told Magento what our Namespace and Module Name is, where to find it, and whether or not to even load it.
However, Magento 2 has shaken things up a bit. Let’s start out by going over a few things that are different. First, as already mentioned, Magento has removed the module declaration folder and files located at app/etc/modules. Secondly, they have also removed the local and community folders. If you are like me, that last one may come as a shock. Even so, it is true.
So how do we declare our module? Well, even though the method is different, it is still quite simple. Everything is now contained inside the app/code folder. In this tutorial I will not be going over the changes to the core files that are located here but simply how and where we can declare our new module. Let’s pretend our new module will be called HelloWorld and our company is Beckin.
Let’s start out by creating our Namespace and Module folders.
Create a new folder called Beckin inside the app/code folder.
Next, create another folder inside the Beckin folder called HelloWorld.
Finally, lets create a new folder located within our Module called etc.
Your new folder structure should look exactly like the following:
Here is where you will see the new change that allows us to declare our Module and tell Magento that it exists. In the past, our etc folder would commonly contain a config.xml, system.xml, and a adminhtml.xml file. In Magento 2, they have added a new file which will declare our module. To do this, we need to create a new file called module.xml and place it inside our etc folder. Next we need to add the following to our module.xml file:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/Magento/Module/etc/module.xsd"> <module name="Beckin_HelloWorld" version="184.108.40.206" active="true"/> </config>
Finally, refresh your Magento 2 installation and navigate to Stores > Configuration > Advanced. Now if you look inside Disable Modules Output tab, you should see our new module.
Lets go over some further options for our module.xml file. As you can see in the above example, all we need to declare our module is the module name, version, and wether or not it is active. But what if our module needs to depend on another module? In Magento 2 we need to be aware of two different types of dependencies.
- Hard dependency implies that a module cannot function without modules, on which it depends. Specially:
- The module contains code that uses logic from another module directly, that is, the latter’s instances, class constants, static methods, public class properties, interfaces, and traits.
- The module contains strings that include class names, method names, class constants, class properties, interfaces, and traits from another module.
- The module deserializes an object declared in another module.
- The module uses or modifies the database tables used by another module.
- Soft dependency implies that a module can function without other modules, on which it depends. Specially:
- The module directly checks another module’s availability.
- The module extends another module’s configuration.
- The module extends another module’s layout.
Furthermore, Magento 2 has added another option called sequence. Sequence is the node that Magento 2 uses to determine the load order of our modules. Let’s take a look at the core module CatalogSearch and take a peek at it’s module.xml file.
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/Magento/Module/etc/module.xsd"> <module name="Magento_CatalogSearch" version="220.127.116.11" active="true"> <sequence> <module name="Magento_Catalog"/> </sequence> <depends> <module name="Magento_Catalog"/> <module name="Magento_Customer"/> <module name="Magento_Core"/> <module name="Magento_Directory"/> <module name="Magento_Index"/> <module name="Magento_Eav"/> <module name="Magento_Backend"/> <module name="Magento_Theme"/> </depends> </module> </config>
Here we can easily assume that Magento needs the Magento_Catalog module to load before the Magento_CatalogSearch module. After all, how can we use the search function without first having something to search? Finally, we can see that this module also depends on on other modules to ensure it can work properly.
I really hope you have enjoyed this tutorial and it has helped you to better understand how to declare your module and how to use the module.xml file. Please feel free to post comments below.
I have been working with Magento for the past 2 years and started creating modules long ago, but recently when the Magento 2 was launched and I was looking for help in creating module in Magento 2, Your post was written in good manners which helped me to complete the task. Hope this post will also help your readers.
You are very welcome! More updated Magento 2 posts will be coming soon so keep an eye out for those.