How To Create a Basic Module in Magento 2?

How To Create a Basic Module in Magento 2?

In Magento 2, modules will be in app/code directory of a Magento installation, with this format: app/code/<Vendor>/<ModuleName>.

Before we start a Magento 2 module development, there are two things people often forget and we recommend you to do:
1. Disable Magento cache
The easiest way to disable cache is to go to Admin → System → Cache Management → select all cache types and disable them.
2. Switch Magento into a developer mode
To run this command, open your terminal and go to the Magento 2 root.
php bin/magento deploy:mode:set developer
# Module Setup:
In this module, we will use Mymodule for Vendor name and HelloWorld for ModuleName.
So we folder structure will look like this:

Step1:  Module.xml
We need create a module.xml file in app/code/Mymodule/HelloWorld/etc
Create a etc folder inside Helloworld(ModuleName) & create a module.xml file

(Paste the code in module.xml)

<?xml version=”1.0″?>
<config xmlns:xsi=”” xsi:noNamespaceSchemaLocation=”urn:magento:framework:Module/etc/module.xsd”>
<module name=”Mymodule_Helloworld” setup_version=”1.0.0″>

Step2:  Registration.php

To register the module, create a registration.php file in the app/code/Mymodule/Helloworld folder with the following code:
(Paste the code in registration.xml)
Step 3 : Assign permission to your root folder & Upgrade Module
Open your terminal and go to the Magento 2 root. Run from there the following command:
$ sudo chmod -R 777 MagnetoRoot
sudo php bin/magento setup:upgrade
After upgrading the terminal will ask for compile command:
bin/magento setup:di:compile
Now you can check your module is present in the list app/etc/config.php Named as ‘Mymodule_Helloworld’
Step 4: Create controller
Create the file index.php in app/code/Mymodule/HelloWorld/Controller/Index.
Index: name of action file – index.php
(Paste the code in index.php)
namespace Mymodule\Helloworld\Controller\Index;

use Magento\Framework\App\Action\Context;

class Index extends \Magento\Framework\App\Action\Action
protected $_resultPageFactory;

public function __construct(Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory)
$this->_resultPageFactory = $resultPageFactory;

public function execute()
$resultPage = $this->_resultPageFactory->create();
return $resultPage;
Step 5 : Create 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/Mymodule/Helloworld/Block folder
(Paste the code in Helloworld.php)

namespace Mymodule\Helloworld\Block;

class Helloworld extends \Magento\Framework\View\Element\Template
public function getHelloWorldTxt()
return ‘Hello world!’;

Step 6: Create layout and template files
Layout file – app\code\Mymodule\Helloworld\view\frontend\layout\helloworld_index_index.xml
(Paste the code in helloworld_index_index.xml)
<?xml version=”1.0″?>
<page xmlns:xsi=”” layout=”1column” xsi:noNamespaceSchemaLocation=”urn:magento:framework:View/Layout/etc/page_configuration.xsd”>
<referenceContainer name=”content”>
<block class=”Mymodule\HelloWorld\Block\HelloWorld” name=”toplinks” template=”Mymodule_HelloWorld::helloworld.phtml”/>

Template File –  app/code/Mymodule/HelloWorld/view/frontend/templates/helloworld.phtml

<h1>Welcome to Magento 2</h1>
<h1><?php echo $this->getHelloWorldTxt(); ?></h1>

Step 7: Again follow step 3

Clear cache : php bin/magento cache:clean

And Set permission to root folder

For more information, please visit Nevpro Business Solutions today!

Watch our videos on YouTube.