How to create a new Hello world simple Magento 2 Module ?

How to create a new Hello world  simple Magento 2 Module ?
September 3, 2017 669 view(s)
How to create a new Hello world simple Magento 2 Module ?
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 Magezend  and module name is HelloWorld. So we need to create a new folder:         app/code/Magezend/HelloWorld

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/Magezend/HelloWorld/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/Magezend/HelloWorld/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 Magezend_HelloWorld
  • Disable module:
    • PHP bin/magento module:disable Magezend_HelloWorld
  • 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 Magezend_HelloWorld

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/Magezend/HelloWorld/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/Magezend/HelloWorld/Controller/Index/Index.php
  • Add the content:

Result: 

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

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.