Here the following steps explain you how to Form a Module in Magento 2:
Step 1: Form a module folder:
Step 2: State the module by module.xml
Step 3: Register the module by registration.php
Step 4: Install Setup, allow the module
Step 5: Form a route for the module.
Step 6: Form controller and action.
There are some changes in Magento 2 to form a block. Form a block in Magento 2 is now a bit different from Magento 1. Of course, you need to know about it.
Step 1: Form a module folder:
We utilize module vendor Webnexs and module name is Hello Magento. So we require form a new folder: app/code/Webnexs/HelloMagento
Step 2: State the module by module.xml
- We need form a configuration in module etc directory. Magento 2 will use it to know the module’s name and module’s version
- app/code/Webnexs/HelloMagento/etc/module.xml
- include this content to state module name is HelloMangento and version 1.0.0
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Webnexs_HelloMagento" setup_version="1.0.0" /> </config>
Step 3: Register the module by registration.php
This file will be formed in magento root folder:
app/code/Webnexs/HelloMagento/registration.php
Include this content to register the module:
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Webnexs_HelloMagento', __DIR__ );
Step 4: Install Setup, allow the module
After form all the above formats. You can install volumes by command line. Please open your terminal and use these commands:
cd [magento_directory] php bin/magento setup:upgrade
Moreover, we can use some commands to sight disabled module list, permit or disable a module:
- Analysis disabled modules:
php bin/magento module:status
- Permit module:
PHP bin/magento module:enable Webnexs_HelloMagento
- Disable module:
PHP bin/magento module:disable Webnexs_HelloMagento
Note: If you use xampp in ubuntu, please type the correct directory for using php command.
Example:/opt/lampp/bin/php bin/magento module:disable Webnexs_HelloMagento
Step 5: Form a route for the module.
- The magento 1 and magento 2 use this design: URL:http://<magento_url>.com/<router_name>/<controller_name>/<action_name>
Example: http://<magento_url>.com/customer/account/create
- So we require init router name for the module before forming any controllers and actions in the future.
- Form a routers.xml file:
app/code/Webnexs/HelloMagento/etc/frontend/routes.xml
- Include this content:
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd"> <router id="standard"> <route id="Webnexs" frontName="hellomagento"> <module name="Webnexs_HelloMagento" /> </route> </router> </config>
Step 6: Form controller and action.
- In the final step, we will form url for showing in browser: “Hello Magento 2. We will modify the world”.
- Form an action file:
app/code/Webnexs/HelloMagento/Controller/Index/Index.php
- Include the content:
<?php namespace Webnexs\HelloMagento\Controller\Index; class Index extends \Magento\Framework\App\Action\Action { public function __construct( \Magento\Framework\App\Action\Context $context) { return parent::__construct($context); } public function execute() { echo 'Hello Magento 2! We will change the world!'; exit; } }
Result:
Open your browser, type this link: http://<magento_url>/hellomagento/index/index and look at the result:
The above mentioned steps explain you to Form a new module in magento 2. Hope all you guys can study magento 2 more easily with tutorial.
Leave a Reply