---
title: "Learn How to make a Component in Magento 2"  
description: "Module is an integral part of a program which includes one or more routines. Component is a couple of parts or devices that can be used to construct a"  
author: "Cao Quang"  
published: 2017-03-13  
updated: 2018-03-27  
canonical: https://www.mindstick.com/articles/12262/learn-how-to-make-a-component-in-magento-2  
category: "magento"  
tags: ["magento"]  
reading_time: 4 minutes  

---

# Learn How to make a Component in Magento 2

Module is an integral part of a program which includes one or more routines. Component is a couple of parts or devices that can be used to construct a far more complex framework. In MVC software, all the controllers and models have their split folders. All data are combined mutually predicated on their functionalities and it is called Modules.

\
To add custom-made [functionality](https://www.mindstick.com/blog/136/using-watermark-functionality-in-textbox-by-jquery) to your Magento, custom component is required. In such a tutorial, I am going to teach you how to create a module in Magento 2.

![Learn How to make a Component in Magento 2](https://magenticians.com/wp-content/uploads/2017/03/How-to-craete-module-in-Magento-2-Banner.jpg)Before creating the module, it's essential to understand the dissimilarities in directory composition between the two variants of Magento. Within the Magento 2 website directory structure, code pools are taken off the file framework. In Magento 1, you can create modules in app/code/local but in Magento 2 you can create a component in app/code.\
To learn more about Magento 1 component, you can go to 'magebay.com'

## Create Directories

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

\

## ![directory structure](https://magenticians.com/wp-content/uploads/2017/03/directory-structure.png)

\

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](https://www.mindstick.com/forum/155707/what-is-the-usage-of-asp-dot-net-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](https://www.mindstick.com/articles/75/how-to-read-and-write-xml-file-through-c-sharp).

View/Frontend/Templates: Contain .phtml file.

## Configuration of Module

Now [configure your](https://answers.mindstick.com/qa/96281/how-would-you-configure-your-video-setting-in-skype) module. Create module.xml file in app/code/Magenticians/Mymodule/etc and put the following code in this file:

```

```

## [Registration](https://answers.mindstick.com/qa/116207/what-is-lei-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:

\
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:

```
/**
* @var \Magento\Framework\View\Result\PageFactory
*/
protected $resultPageFactory;
/**
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Framework\View\Result\PageFactory resultPageFactory
*/
public function __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
*/
public function execute()

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:

```
public function __construct(
\Magento\Backend\Block\Template\Context $context,
array $data = []
)

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

public function getMymodule()

return 'Module Cretaed 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 title for this page and give the template [file name](https://answers.mindstick.com/qa/95119/what-should-i-do-in-windows-10-if-the-pdf-file-name-is-too-long-and-the-windows-options-don-t-give-me-the-possibility-to-rename-it) in the Block class. Put the following code in it:

```
Magento 2 Module
```

## 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:

## getMymodule(); ?>

## \

## 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](https://magenticians.com/wp-content/uploads/2017/03/Output.png)

**[Final Words](https://answers.mindstick.com/qa/34483/what-are-the-final-words-of-the-face-of-fu-manchu)**

Custom module must [add custom](https://www.mindstick.com/forum/34385/add-custom-css-in-razor-view) operation to your Magento store. After third , tutorial you ought to be able to make a module in Magento 2. When you have any distress or want to add something to the tutorial, simply use the comment section below!

View more our product : https://productsdesignerpro.com/

---

Original Source: https://www.mindstick.com/articles/12262/learn-how-to-make-a-component-in-magento-2

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
