How to Create a Module in Magento 2

0

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.

To learn more about Magento 1 module, you can go to ‘How to Create a Magento 1.9 Custom Module.

Create Module in Magento 2

Create Directories

To create a module in Magento 2, create the directories as shown below:

directory structure

Magenticians: Namespace of Module

Mymodule: Name of module.

Block: Contain PHP files

Controller: Controls the flow of application execution.

Index: Module controller name and it contains the action file.

Etc: Configuration file of module will be placed here.

Etc/Frontend: Router file will be placed here.

View/Frontend: Layout and Templates Folder will be created here.

View/Frontend/Layout: Contain XML file.

View/Frontend/Templates: Contain .phtml file.

Configuration of Module

Now configure your module. Create module.xml file in app/code/Magenticians/Mymodule/etc and put the following code in this file:

Registration of Module

Registration of Module is required. To do this, go to app/code/Magenticians/Mymodule and create registration.php. Put the following code in it:

Front End Router File

The front name in the router file will be used to access the module through the URL. Create routes.xml in app/code/Magenticians/Mymodule/etc/frontend. Put the following code in it:

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:

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:

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:

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.

Output

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!

About Author

Syed Muneeb Ul Hasan is an expert in PHP and Magento, he prefers to educate users in implementing and learning Magento. When not working, he loves to watch cricket.