Tony's Blog

How to create custom module in magento 2

To create Hello World module, you need to complete the following high-level steps:

  • Step 1: Create the folder of Hello World module

    app/code/A/HelloWorld

  • Step 2: Create etc/module.xml file

    app/code/A/HelloWorld/etc/module.xml

  1. <?xml version="1.0"?>
  2. <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
  3. <module name="A_HelloWorld" setup_version="1.0.0">
  4. </module>
  5. </config>
  • Step 3: Create etc/registration.php file
  1. <?php
  2. \Magento\Framework\Component\ComponentRegistrar::register(
  3. \Magento\Framework\Component\ComponentRegistrar::MODULE,
  4. 'A_HelloWorld',
  5. __DIR__
  6. );
  • Step 4: Enable the module
  1. php bin/magento module:status

You should see the module is disable now:
Tony's blog image


Follow exact guide to enable the module right now, let run the command as:

  1. php bin/magento module:enable A_HelloWorld

Your module should be available now.
Tony's blog image

After this step, when you open your website in browser you will get an error saying

Please upgrade your database: Run “bin/magento setup:upgrade” from the Magento root directory.

Let run the command:

  1. php bin/magento setup:upgrade

Finally the fold like this:

Tony's blog image