4.8 (95.78%) 223 votes

In this topic, we will create a module in Magento 2 and learn about Magento 2 modules. It’s following by 6 steps:

  1. Step 1: Create a module folder for Magento 2 modules
  2. Step 2: Declare the module by module.xml
  3. Step 3: Register the module by registration.php
  4. Step 4: Install Setup, Enable the module
  5. Step 5: Create a route for the module
  6. Step 6: Create controller and action

Create a Module has some changes in  Magento 2. Create a Magento 2 Module now have a bit different from Magento 1. And of course, you must learn about it.

Step 1: Create a module folder for Magento 2 Modules

We use module vendor Magestore and module name is HelloMagento. So we need to create a new folder:         app/code/Magestore/HelloMagento

Step 2: Declare the module by module.xml

  • We need create a configuration in module etc directory. Magento 2 will use it to recognize the module’s name and module’s version
    • app/code/Magestore/HelloMagento/etc/module.xml
  • Add this content to declare module name is HelloMangento and version 1.0.0

Step 3: Register the module by registration.php

  • This file will be created in magento root folder:
    • app/code/Magestore/HelloMagento/registration.php
  • Add this content to registered the module:

Step 4: Install Setup, Enable the module

After create all files of above steps. We can install the module through command line. Please open your terminal and use these commands:

Additionally, we can use some commands to view disabled module list, enable or disable a module:

  • View disabled modules:
    • php bin/magento module:status
  • Enable module:
    • PHP bin/magento module:enable Magestore_HelloMagento
  • Disable module:
    • PHP bin/magento module:disable Magestore_HelloMagento
  • Note:  If you use xampp in ubuntu, please type the correct directory for using php command. Example:

/opt/lampp/bin/php  bin/magento  module:disable Magestore_HelloMagento

Step 5: Create a route for the module

  • Both of Magento 1 and Mmagento 2 use this format url:
    • http://<magento_url>.com/<router_name>/<controller_name>/<action_name>
      Example: http://<magento_url>.com/customer/account/create
  • So we need init router name for the module before creating any controllers and actions in the future.
  • Create a routers.xml file:
    • app/code/Magestore/HelloMagento/etc/frontend/routes.xml
  • Add this content:

Step 6: Create controller and action

  • In the last step, we will create url for displaying in your browser: “Hello Magento 2. We will change the world”.
  • Create an action file:
    • app/code/Magestore/HelloMagento/Controller/Index/Index.php
  • Add the content:

Result: 

Open your browser, enter this link: http://<magento_url>/hellomagento/index/index and look at the result:

1

These are all steps to create a new module in Magento 2. Hope all you guys can learn Mmagento 2 more easily with our series lesson. In next topic, we will show you how to create a view, block, template in Magento 2.

The 6 steps I mentioned above is the shortest process for you to Create a Magento 2 module. With this guide, you can manage the Module in Magento 2 easily. Every store has a Module in Magento 2 with many attributes.

Thank you for reading this post.

Your Magento website needs a powerful Magento ERP for stock control.

DON'T MISS THE CHANCE!

magento erp

How to Insert/Update Multiple Records to Database in Magento 2
How to Insert a New Tab in User Editing Page in Magento 2

Leave a Reply

Your email address will not be published. Required fields are marked *