In this article we will discuss how to create a module in Magento 2. Magento 2 modules are the building blocks that make the Magento what it is.

So what is a Magento module?

A module is just a directory with a bunch of files organized in a certain way that make up a specific business feature. Modules ideally encapsulate one specific business features and may dependencies on other modules.

A module is used to to implement a new functionality or extend an existing functionality.

How do I create a module?

To create a new module, follow the steps below:

Create a new directory inside the app/code directory in your Magento 2 root in the format Namespace/Module

  • Namespace - is a unique region where your modules lie. Magento is the namespace used by Magento within which all their code lies.
  • Module - this is your module. All your code will lie in this directory.

Note: You can have multiple modules in a single namespace.

From now on, all the paths below will be relative to your module directory.

Create registration.php file

This file will decide whether your module is picked up by the Magento 2 code as a module or not. If this file is not defined, Magento will just ignore your code.

Add the below code in the file. This tell Magento 2 to register this directory as a module by the name {Namespace}_{Module}

Replace {Namespace} and {Module} with your own identifiers.

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    '{Namespace}_{Module}',
    __DIR__
);

Create etc/module.xml file

This is mainly used to check the version of the current module, and it’s dependencies on other modules. The order of execution will depend on the dependencies that this module has.

Add the following XMl into the file:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="{Namespace}_{Module}" setup_version="1.0.0">
        <!-- optional -->
        <sequence>
            <module name="{NamespaceSameAsAboveOrDifferent}_{SomeOtherModule}"/>
        </sequence>
        <!-- end optional -->
    </module>
</config>

That’s all. You’ve now created a Magento 2 module. Congratulations!

You can now enable the module using the command:

php bin/magento module:enable {Namespace}_{Module}

You will now see this module listed in the array in the file app/etc/config.php

...
    '{Namespace}_{Module}' => 1
...

Next, run the command to register the module in the database:

php bin/magento setup:upgrade

That’s all, your module has been enabled now.

If you see a broken page, you can run the following command to fix it:

php bin/magento setup:static-content:deploy

If you’ve not realised it yet, this module does nothing except being registered as a module. In the next article, I will show you how to add a controller to the module and display a Hello World! message.

Live chat: cookie consent required

Copyright © Hungersoft 2019

· Terms & Conditions · Privacy policy