photo credit: Travel via photopin (license)

Creating a module in Magento 2

A module in Magento 2 is one of the things you’ll encounter (and create) the most when developing with Magento 2. Basically everything in Magento is a module, and if you want to add some custom functionality of your own, chances are that you need to create a module for it.
In this article I’ll explain how to create such a module in Magento 2.

The most basic module ever

Let’s create the most basic module ever. The only thing this module does, is that it exists. It does nothing more, nothing less. Every module should at least consist of 2 files:

  1. A file called etc/module.xml , telling Magento what’s the name and version of itself.
  2. Another file, called registration.php , executing some code with which the module registers itself with Magento.

These files are located in your Magento installation in app/code/(Namespace)/(Modulename)  where Namespace  and Modulename  are of course replaced with something you prefer. For this example, we’ll use the namespace ‘Gielberkers’ and we call our module ‘Example’:
Now we have this sorted out, let’s take a look at these 2 files:


This XML-file is pretty straight-forward. As said before, it only tells us the name and the version of our module. Note the xsi:noNamespaceSchemaLocation -attribute in this document. This can be used in PhpStorm to give code completion for your XML-files.


This file is executed once by Magento 2 when the module gets registered for the first time.

Almost done

Now when this was Magento 1, you’ll be done. You could check if you’re module was picked up by logging in to the admin and check in the configuration by ‘disable module output’ if the name of your module is there.
However, in Magento 2 we need to perform an extra action: we need to explicitly activate our module from the shell. Why? Because the shell is awesome, that’s why! Enter your shell and navigate to the bin  folder in the root of your installation. Make the file magento  in this folder executable (if it’s not already) with:

Now we can execute the following command:

Now this action shows us the status of all available modules. But note the lines on the end:

Aha! Magento did see our module! So the only thing we need to do now is activate it. This can simple be done with the following command:

Once again, our good friend the shell will provide us with all kind of handy information:

Don’t pay to much attention to this for now. The most important thing is that our module is live and kicking at this point! Go ahead, take a quick peak at your disabled modules output configuration thingy:

DERP! What’s this?!? Well, Magento 2 is very strict in it’s module activation stuff. This message tells us that we need to update our database since we’ve enabled a new module. To do this, simply head back to the shell and do as told in the error message:

Once again a lot of text fills your terminal, providing us with feedback. But now everything should be running a-ok. Head back to your admin page and reload it:

Screen Shot 2016-03-15 at 22.03.27
Hurray!!! Our module works! Magento picked it up!

In conclusion

Magento 2 is somewhat stricter in the setup of their modules. Where in Magento 1 you already had a module by simply creating 1 XML-file in app/etc/modules , it takes a few more steps in Magento 2 to create (and activate) an empty boilerplate for your module.
Having said that, Magento 2’s functionality where modules can get activated, deactivated and upgraded from the shell offers a much better interface than Magento 1 had, where you had to depend on a browser refresh or a cache flush on the client / server side. Magento 2 offers with their bin/magento  ‘binary’ a fantastic way to hook into continuous integration environments where an up- or downgrade are part of a deployment or rollback.

This post is part of the series Magento 2 Development from Scratch.

Visitors give this article an average rating of 4.2 out of 5.

How would you rate this article?

One thought on “Creating a module in Magento 2”

  1. Alex says:

    Thanks for sharing this post, when i was stuck in creating controller, your guide helped me a lot, but for structure of module i followed this post, https://www.cloudways.com/blog/create-module-in-magento-2/

Leave a Reply