How to create Basic Module in Magento 2

 In Magento blog
Here, you can learn basic module in Magento 2, when you learn Magento 2 you need to learn Hello World basic module step by step and it’s mandatory.

Let’s start with different things to create a custom module in Magento 2:

First of all you need to set developer mode. So, check status for deploy mode it’s default, developer or production using below command line.

If you see the default or production mode in your result then you can switch to developer mode using below command line:

Now, you need to create Module Setup as per the following:

  1. Create Folder: app/code/VendorName/Helloworld
    – Here VendorName is module namespace and Helloworld is module’s name.
  2. Create module.xml file in app/code/VendorName/Helloworld/etc folder with the following code.
  3. Create registration.php file in app/code/VendorName/Helloworld folder to register the module with the following code.
  4. Now, open your terminal and run the following command in your Magento root folder.
  5. Make sure your module is installed or not with the following command and check your module with enable/disable list.
    Or you can also check VendorName_Helloworld with value 1 in app/etc/config.php file.

Create the controller with the following step:

  1. Define the route with routes.xml file in app/code/VendorName/Helloworld/etc/frontend folder with the following code:
    Here, define the frontend router with frontName helloworld. frontName attribute as part of URL.

    • In Magento 2 URL’s is:
  2. Create index.php file in app/code/VendorName/Helloworld/Controller/Index folder with the following code:
    In Magento 2 every action has own class which implements the execute() method.

Create the block with the following code:

  1. Create Helloworld.php block file in the app/code/VendorName/Helloworld/Block folder with the following code:

Create the layout and template file with the following step:

  1. Here, three subfolders inside the view(app/code/VendorName/Helloworld/view) folder: adminhtml, base and frontend.
    • Create helloworld_index_index.xml file in the app/code/VendorName/Helloworld/view/frontend/layout folder with the following code:
      Our controller handle is helloworld_index_index. You may create the layout configuration file for every layout handle.
    • Create helloworld.phtml file in the app/code/VendorName/Helloworld/view/frontend/templates folder with the following code:

Run the Magento Upgrade and cache clean command as per following:

Now, open the URL yourdomain/helloworld/index/index in your browser and you should get something like this:

We hope our technical blog which looking is very helpful for you. If you have any queries, feel free to leave a comment below.

Leave a Comment