Friday, April 10, 2020

How do you create a custom module in Magento?


The module is a structural element of Magento 2 – the whole system is built upon modules. Typically, the first step in creating customization is building a module.
To create a module, you need to complete the following high-level steps:
  1. Create a module folder.
Depending on how Magento 2 has been installed, core modules can either be located in the vendor/magento/magento-*folders (for composer installation) or in the app/code/Magento/ folder
Let’s create the folder app/code/Learning and inside this folder place another folder: FirstUnit. If you’re using the command line, the code would be:

  1. cd to the root folder

  2. mkdir app/code/Learning

  3. mkdir app/code/Learning/FirstUnit
2. Create the etc/module.xml file.
This file contains the following information:
  • Module name
  • Module version
  • Dependencies
Using the following command-line code, create the folder app/code/Learning/FirstUnit/etc:

  1. mkdir app/code/Learning/FirstUnit/etc
Then put the following code into it:

  1. <?xml version="1.0"?>

  2. <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">

  3. <module name="Learning_FirstUnit" setup_version="0.0.1"> <sequence>

  4. <module name="Magento_Catalog"/> </sequence>

  5. </module>

  6. </config>
3. Create the registration.php file.
create the file app/code/Learning/FirstUnit/registration.php. Then put the following content into it:

  1. <?php \Magento\Framework\Component\ComponentRegistrar::register(

  2. \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Learning_FirstUnit',

  3. __DIR__

  4. );
4. Run the bin/Magento setup:upgrade script to install the new module.

  1. php bin/magento setup:upgrade
5. Check that the module is working.

  1. grep Learning_FirstUnit app/etc/config.php
These are just the basic steps that you need to follow for creating a module in Magento 2. Although, if you are a beginner then you need to understand and study the process beforehand to avoid errors. If you are looking for guidance then consulting a company providing Magento 2 development services is a good idea.