Magento 2 – Register a Custom Module

Magento 2 Register Custom Module

Within Magento 2, we can create custom modules that are built on top of the original framework. Modules allow us to extend, override existing functionality or create new functionality altogether.

So how do we go about registering a module through Magento 2? Firstly, modules should belong in the app/code directory and similar to Magento 1, contain a namespace (commonly known as a vendor name) and module name. Unlike Magento 1, there are no code pools such core, community and local within Magento 2.

For the purpose of this article, we’ll be creating a Sp_Custommodule module.

To start with, the module should be declared. This is done in the module’s module.xml file that resides within the module’s etc directory.


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Sp_Custommodule" setup_version="0.0.1" />
</config>

If your module is dependent on an existing module, you can add the dependency by using a pair of <sequence> nodes.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Sp_Custommodule" setup_version="0.0.1">
        <sequence>
            <module name="[Vendor]_[Module]"/>
        </sequence>
    </module>
</config>

Register the module using the registration.php file. You can copy the configuration from an existing Magento module if needed. The only edit you’ll need to make is adding the name of your module as the second argument of the register() method.


<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Sp_Custommodule',
    __DIR__
);

Then, run the following command to enable your module.

$ /path/to/your/php bin/magento module:enable --clear-static-content Sp_Custommodule

At this point, Magento may throw a warning prompting you to run the database upgrade command. This is because Magento has detected that there is a version number of the module in the code but not a version number saved within the setup_module database table.

Run the database upgrade command below.

$ /path/to/your/php bin/magento setup:upgrade

Within the admin panel, you can check if Magento has correctly identified your module by heading to Stores -> Configuration -> Advanced -> Advanced. If you have configured your module correctly, you should see it appear within the module list.

Magento 2 Register Module

That is the basics of registering a custom module. Remember, Magento is modular, and the core framework should not be touched. Therefore you can now correctly extend, override existing functionality or create new functionality altogether using this module.

Note: This article is based on Magento version 2.1.