In this lesson we will learn how to register a new module with Magento 2. This will cover some of the basic folder structures and how they differ from Magento 1.x
A Vendor name is simply a name that uniquely identifies you and where any modules that you create will reside. For example a company name or an alias you like to use. In this case we will use our company name Iweb.
Note: It is a good idea to choose a vendor name relevant to you to avoid clashes with other companies / individuals.
Similarly, a module name is just a name that briefly describes what the module does or contains. As this is just a tutorial we will use HelloWorld.
Magento follows a preferred structure of code that we will discuss here.
In Magento 1.x we have three code pools. ‘local’ for modules specific to a store, ‘community’ for modules designed to be distributed to the community or other local stores and ‘core’ for all the Magento core functionality shipped with Magento itself.
Magento 2 no longer uses code pools. Here is roughly how your module structure will look in Magento 2.
Given your chosen vendor and module name, you should go ahead and create the following structure <root>/app/code/<Vendor>/<ModuleName>/etc which in our case would be <root>/app/code/Iweb/HelloWorld/etc
You will notice i have included the creation of a folder called ‘etc’. This is important and like in Magento 1.x is where all your module configuration goes with a number of subtle differences.
Firstly, declaring your module is now done in a file called module.xml inside this directory replacing the app/etc/modules/<Vendor>_<ModuleName>.xml you had to declare in Magento 1.x.
Secondly, as you come to expand your module further, other configuration files will be put in here. For example, if you would like to observe an event in Magento 2, a file called events.xml will need to exist letting Magento know what events you want to observe. Given that scenario you would end up with the following structure;
It is also possible to provide configuration for the different areas of Magento (global, admin and frontend). This is out of the scope of this tutorial but it does highlight that the file structure is much more granular in Magento 2 which I love. It enables you to see without opening the files what kind of things are declared rather than having a single config.xml file (like in Magento 1.x) that houses 95% of the modules configuration and is not declared in any particular order.
Go ahead and create a module.xml inside the etc folder we have just created.
Open up the xml file and paste in the follow contents:
Make sure to replace [Vendor]_[ModuleName] with Iweb_HelloWorld or whatever your vendor and module name might be.
You now have the files Magento needs to read your module but it still does not know it exists.
There are a number of ways you can tell Magento about your module. The first is to manually edit the app/etc/config.php file. Open the file and you will see a long list of modules Magento already knows about. You need to add your new module to the bottom of this list. It should look something like the following where … signifies other modules declared in the file;
If however, you are a little more confident with scripts on the command line, Magento provides a handy command you can use to do this for you. From within the <root> folder run `bin/magento module:enable <Vendor>_<ModuleName>` or in our case `bin/magento module:enable Iweb_HelloWorld`.
This will add it to the config.php file for you and conveniently clear any config cache so Magento will see your module.
How can we check that this has worked though.
Magento provides a nice admin screen that shows all of the modules Magento has been told about and has successfully managed to find. To see this, log in to the admin and go to stores → settings → configuration → advanced. The module should be listed here under the name of Iweb_HelloWorld.
That’s it, done. Ok so this module doesn’t actually do anything at the moment but you can see how easy it is to register a new module with Magento 2 🙂