A Magento extension is a block of PHP code – a combine of functions, added into Magento to extend its functionality.
Magento extensions allow you to add custom features and functionality to every area of your Magento store including the front and back end, integrations with other web services, marketing tools, themes and more. Extensions are developed through broad network of Magento partners to give you the flexibility and power to maintain your store the way you want . As you can see, if you want to create an extension , you have to have a lot of knowledge such as PHP, Mysql, Javascript, VPS … . Don’t worry about that. In this tutorial , I will introduce step by step How to Build Magento 2 Extension.
To create a Extension , you need to complete the following high-level steps:
- Create the module folder.
- Create the
etc/module.xml
file. - Create the
registration.php
file. - Run the
bin/magento setup:upgrade
script to install the new module. - Check that the module is working
Step 1 : Create module folder in app/code/ You can see structure extension on Screen image
Let’s go through each of these steps in detail.
All extension code is place in app/code/VendorName/ModuleName . Example app/codeMagebay/Hello/
Step 2: create an etc/module.xml file. This file is required for the module to exist.
File : app/code/YourVendor/YourModule/etc/module.xml
app/code/Magebay/Hello/etc/module.xml
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"> <module name="Magebay_Hello" setup_version="2.1.5" active="true"> <sequence> <module name="Magento_Catalog"/> </sequence> </module> </config>
Note that in the XML file we specified:
- Module name: Magebay_Hello (based on the folders we created) Module name is defined by the folders we just created, because in Magento 2, class names must follow the folder structure. For example: Magebay\Hello\Controller\Index
- Version: 2.1.5 (initial version of our module)
Module version indicates the current version of the database schema and data, and is used in upgrading, For example , if you want to add new table or new fields in table, You will increase version in this file and version in file Setup/UpgradeSchema.php . I will explain more in next step. - Dependency: Magento_Catalog. We could have multiple dependencies. In this case, we would put nodes under the sequence node.
Step 3; Create registration.php file
You have yo create file registration.php file app/code/YourVendor/YourModule/registration.php
app/code/Magebay/Hello/registration.php
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Magebay_Hello', __DIR__ );
Step 4 : Run php bin/magento setup:upgrade to install extension.
Step 5 : Check Module working
You have done simple module , You can create new controller to test.
Configure Router. add etc/frontend/routers.xml
File:
app/code/YourVendor/YourModule/etc/frontend/routers.xml
app/code/Magebay/Hello/etc/frontend/routers.xml
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd"> <router id="standard"> <route id="hello" frontName="hello"> <module name="Magebay_Hello" /> </route> </router> </config>
File : app/code/YourVendor/YourModule/Controller/Index/Index.php
app/code/Magebay/HelloWorld /Controller/Index/Index.php
<?php namespace Magebay\Hello\Controller\Index; use Magento\Framework\App\Action\Context; use Magento\Framework\View\Result\PageFactory; class Index extends \Magento\Framework\App\Action\Action { public function __construct( Context $context, PageFactory $resultPageFactory ) { parent::__construct($context); $this->resultPageFactory = $resultPageFactory; } public function execute() { echo "module install"; } }
Thank For reading The post, I will introduce how to add database and get it to frontend in next post, if you have any questions about How to Build Magento 2 Extension , you can comment under post so I will check and reply to you.
You make clearer points than the usual…