A Complete Tutorial on Magento 2 Module Development
“Hello World” is where a new programmer is born! 😄
Starting with the ABCD of Magento 2 development, hello world module is mandatory!
So, here I am, with a post for newbies to create a simple module in Magento 2, for hello world. Magento 2 module development is a stepwise process and needs to be done with some prerequisites conditions.
Follow the below tutorial and create your own basic module in Magento 2!
Things to take care of before you create a custom module in Magento 2:
Switch to developer mode: Switch to developer mode in order to see every error Magento is throwing at you. Use the below command:
1 |
php bin/magento deploy:mode:set developer |
Learn more about the Magento 2 Modes
Method for Magento 2 Module Development:
Getting to each step,
-
Module Setup
- Create the below folders
- app/code/Meetanshi
- app/code/Meetanshi/Helloworldwhere Meetanshi folder is the module’s namespace and our module’s name is Helloworld!Note: You’d be required to create the “code” folder manually if it is not in the “app” directory.
- Create a module.xml file in the app/code/Meetanshi/Helloworld/etc folder with the below code:
123456<?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="Meetanshi_Helloworld" setup_version="1.0.0"></module></config> - Create a registration/php file in the app/code/Meetanshi/Helloworld folder to register the module. Implement the below code:
1234567<?php\Magento\Framework\Component\ComponentRegistrar::register(\Magento\Framework\Component\ComponentRegistrar::MODULE,'Meetanshi_Helloworld',__DIR__); - Open the terminal and go to the Magento 2 root. Run the below command
1php bin/magento setup:upgrade
- Create the below folders
-
Creating a Controller
- Define the router with routes.xml file in the app/code/Meetanshi/Helloworld/etc/frontend folder with the below code:
123456789<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd"><router id="standard"><route id="helloworld" frontName="helloworld"><module name="Meetanshi_Helloworld" /></route></router></config> - Create the Index.php file in the app/code/Meetanshi/Helloworld/Controller/Index folder with the below code:
123456789101112131415161718192021222324252627<?phpnamespace Meetanshi\Helloworld\Controller\Index;use Magento\Framework\App\Action\Context;use Magento\Framework\App\Action\Action;use Magento\Framework\View\Result\PageFactory;class Index extends Action{protected $_resultPageFactory;public function __construct(Context $context,PageFactory $resultPageFactory){$this->_resultPageFactory = $resultPageFactory;parent::__construct($context);}public function execute(){$resultPage = $this->_resultPageFactory->create();return $resultPage;}}
In Magento 2 every action has its own class which implements the execute() method.
- Define the router with routes.xml file in the app/code/Meetanshi/Helloworld/etc/frontend folder with the below code:
-
Creating a block
Create a simple block class with the getHelloWorldTxt() method which returns the “Hello world” string.
- Create a Helloworld.php file in the app/code/Meetanshi/Helloworld/Block folder with the below code:
123456789101112<?phpnamespace Meetanshi\Helloworld\Block;use Magento\Framework\View\Element\Template;class Helloworld extends Template{public function getHelloWorldText(){return 'Hello world!';}}
- Create a Helloworld.php file in the app/code/Meetanshi/Helloworld/Block folder with the below code:
-
Creating a layout and template files
Layout files and templates are placed in the view folder inside the module. We can have three subfolders inside the view folder: adminhtml, base, and frontend.
- Use the below code to create a helloworld_index_index.xml file in the app/code/Meetanshi/Helloworld/view/frontend/layout folder:
1234567<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd" layout="1column"><body><referenceContainer name="content"><block class="Meetanshi\Helloworld\Block\Helloworld" name="helloworld" template="helloworld.phtml" /></referenceContainer></body></page>Every page has a layout hand. For our controller action, the layout handle is helloworld_index_index. You may create a layout configuration file for every layout handle. In our layout file, we have added a block to the content container and set the template of our block to helloworld.phtml, which we create in the next step.
- Create a helloworld.phtml file in the app/code/Meetanshi/Helloworld/view/frontend/templates folder
12<h1><?php echo $this->getHelloWorldText(); ?></h1><!-- Display Text from block file -->
$this variable refers to our block class. Our method getHelloWorldTxt() returns the string “Hello world!”
- Use the below code to create a helloworld_index_index.xml file in the app/code/Meetanshi/Helloworld/view/frontend/layout folder:
Open the /helloworld/index/index URL in the browser to check the below output.
That’s it with the custom Magento 2 module development! Also validate condition rules in custom module for Magento 2 using ruleFactory enhances e-commerce customization and efficiency, allowing dynamic promotions and improving the user experience.
Learn to check Magento 2 coding standards using code sniffer.
Hopefully, the post helps you go through the first Magento 2 module development stage! If you get stuck in the between, make sure to solve your doubts using the Comments section below. I’d be happy to help 🙂
Do rate the post with 5 stars!
Thank you.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
11 Comments
but where the module is showing on homepage??? i mean it should be seen on the home page with module name “my module” and after clicking it redirect me to /helloworld/index/index
Hey Ankush,
No in frontend Module name is not shown,
but using router url like domain/helloworld/index/index it will redirect your custom page
and show text like ‘Hello world!’
Thank You!
not working
Please do let us Know, What is the Specific issue You are Facing ?
Parse error: syntax error, unexpected ‘ ‘ (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) in C:\xamppp\htdocs\magento\app\code\Magegk\Newmodule\Controller\Index\Index.php on line 11
I got this error please help to remove it..
Hello Komal,
There might be an issue of module name
Try Meetanshi_Helloworld as shown in the above code.
Thank You
Thanks, Sanjay.
It was useful.
Hey Rajan, I’m glad the post was helpful.
Thanks for the appreciation 🙂
Why not added composer.json?
Hello,
Custom module may require it but this is an example of a simple “Hello World” module and hence not added.
Thanks.
For implementing customization to Magento store, it is highly recommended to create custom module to override core files rather than editing them directly. Thanks for sharing this great tutorial.