Magento 2 Module Basics
Let’s do this!
The aim here is just to get a module registered with Magento 2. Nothing fancy.
Assuming you now have a installation of Magento 2 (if not see Creating a Magento 2 project with Composer), the first job is to let it know about your new module. You can do this with just two files.
Firstly create your module structure; app/code/[codePool]/[Vendor]/[ModuleName]
Hang on one second! What is [Vendor] and [ModuleName]? [Vendor] is your unique name. It defines your namespace. In my case, I use “Smartie”. [ModuleName] is erm, well, your module name :) From here on in, anytime you see [Vendor] and [ModuleName], you should replace it with your details.
Ok so we have our basic module set up with all the necessary files in place BUT, Magento is not magic. We still need to ask it to activate our module.
bin/magento module:enable [Vendor]_[ModuleName]
Great, Magento now knows about our module. In fact, if you look in app/etc/config.php, you should see your module in there. A bit like;
After you ran the previous command, you may have noticed Magento asking you to run another command;
That does a lot of things in the background but for now all we need to know is that it makes an entry in the setup_module table
There are a number of ways to check that your module is now registered with Magento. My preferred way is to run;
Do that and you should see your new module appear in the list of enabled modules.
If we want to be able to install our module via composer later on, we need to create app/code/[codePool]/[Vendor]/[ModuleName]/composer.json
Note the lowercase [vendor] and [modulename] in the composer file.
Honestly though, the “name” can be anything you like. Magento for example like to prefix it with “module-“. So, “magento/module-catalog” for example
Don’t forget to commit your handy work #vcs
Like this post? Share it :)