Follow the below steps to create a Magento 2 module – PHPcodez_First
Step 1: Create the folder for the module
Step 2: Create etc/module.xml file
Step 3: Create etc/registration.php file
Step 4: Enable the module
Step 1: Create the folder of Hello World module
Name of the module is defined as “VendorName_ModuleName”. First part is name of the vendor and last part is name of the module. Focus on following guide to create the folders:
app/code/PHPcodez/First
Step 2: Create etc/module.xml file.
Then, it is necessary to create etc folder and add the module.xml file
app/code/PHPcodez/First/etc/module.xml
<?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="PHPcodez_First" setup_version="1.0.0"> </module> </config>
Step 3: Create etc/registration.php file
In this step, we will add registration.php as following guide:
app/code/PHPcodez/First/registration.php
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'PHPCodez_First', __DIR__ );
Step 4: Enable the module
When step 3 is done , we have already created the First module. Now we should enable this module by running the following command.
php bin/magento module:enable PHPCodez_First
Now we should upgrade Magento database by running the following command.
php bin/magento setup:upgrade
After this we should deploy the static content
php bin/magento setup:static-content:deploy
Once deployment is done, you can also see your module from backend at Store -> Configuration -> Advanced -> Disable Modules Output.
Now, we will create a controller to test the module and for that we should create a route for this module.
Routes in Magento are divided into 3 parts: Route frontname, controller and action as following
example:
http://phpcodez.com/index.php/frontname/controller/action
To add route, it is necessary to create routes.xml file
app/code/PHPCodez/First/etc/frontend/routes.xml
since this is a frontend route, we added it in frontend/ folder else we need to add it to adminhtml/ folder
<?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 frontName="first" id=" first "> <module name="PHPCodez_First "/> </route> </router> </config>
Now we will continue with the controller and action
The folder and file you need to create is: app/code/PHPCodez/First/Controller/Index/First.php
<?php namespace PHPCodez\First\Controller\Index; class Test extends \Magento\Framework\App\Action\Action { protected $_pageFactory; public function __construct( \Magento\Framework\App\Action\Context $context, \Magento\Framework\View\Result\PageFactory $pageFactory) { $this->_pageFactory = $pageFactory; return parent::__construct($context); } public function execute() { echo " PHPCodez - Your First Module Is Ready. Cheers !!! "; exit; } }
Once its done, please run php bin/magento cache:clean to check result.
Your URL now should be as http://phpcodez/first/index/first
Now the message ‘PHPCodez – Your First Module Is Ready. Cheers !!!’ will be displayed on the browser