How to Create a Module in Magento 2

A Module is a part of a program which has one or more routines. A Module is a set of parts or units that can be used to construct a more complex structure. In MVC application, all the controllers and models have their separate folders. All files are combined together based on their functionalities and it is called Modules.

Custom module is required to add customized functionality to your Magento. In this tutorial, I will teach you how to create a module in Magento 2.

Before creating the module, it’s necessary to understand the differences in directory structure between the two versions of Magento. In the Magento 2 directory structure, code pools are removed from the file structure. In Magento 1, you can create modules in app/code/local but in Magento 2 you can create a module in app/code.

Create Controller

To create controller class for each action, you have to create separate files for each. Now we will create Index.php file in app/code/Magenticians/Mymodule/Controller/Index. Put the following code in it:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

<?php

namespaceMagenticians\Mymodule\Controller\Index;

classIndexextends\Magento\Framework\App\Action\Action

{

/**

* @var \Magento\Framework\View\Result\PageFactory

*/

protected$resultPageFactory;

/**

* @param \Magento\Framework\App\Action\Context $context

* @param \Magento\Framework\View\Result\PageFactory resultPageFactory

*/

publicfunction__construct(

\Magento\Framework\App\Action\Context$context,

\Magento\Framework\View\Result\PageFactory$resultPageFactory

)

{

$this->resultPageFactory=$resultPageFactory;

parent::__construct($context);

}

/**

* Default customer account page

*

* @return void

*/

publicfunctionexecute()

{

return$this->resultPageFactory->create();

}

}

?>

Create Block

As the block contains PHP view classes, so now we will create Mymodule.php file in app/code/Magenticians/Mymodule/Block. Put the following code in it:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<?php

namespaceMagenticians\Mymodule\Block;

classMymoduleextends\Magento\Framework\View\Element\Template

{

publicfunction__construct(

\Magento\Backend\Block\Template\Context$context,

array$data=[]

)

{

parent::__construct($context,$data);

}

publicfunctiongetMymodule()

{

return'Module Created Successfully';

}

}

?>

Frontend layout File

Create mymodule_index_index.xml in app/code/Magenticians/Mymodule/view/frontend/layout. This (mymodule_index_index.xml) file is a frontend layout file in which I will write the title for this page and give the template file name in the Block class. Put the following code in it:

Frontend Template File

Now create a template file which will be used to call block function. Create mymodule.phtml in app/code/Magenticians/Mymodule/view/frontend/templates. Put the following code in it:

1

2

3

<h2>

<?phpecho$block->getMymodule();?>

</h2>

Module Activation

All done! Now just activate the module by running the commands in your Magento’s root directory:

1- Check the status of your module:

php bin/magento module:status

2- If your module is in the disabled list then just run this command to enable it otherwise move to step 3 :

php bin/magento module:enable Magenticians_Mymodule

3- Now upgrade the setup:

php bin/magento setup:upgrade

4- At last, clean the cache:

php bin/magento cache:clean

php bin/magento cache:flush

Launch the browser, enter your store URL , and add /mymodule or /mymodule/index/index at the end of URL.

Final Words

Custom module is required to add custom functionality to your Magento store. After following this tutorial you should be able to create a module in Magento 2. If you have any confusion or want to add something to the tutorial, simply use the comment section below!