Here we go to create your first or very simple module in magento2.
We are going to create module with Namespace is “Jwd” and Module Name is “WelcomeWorld”.
Step1: Create a module.xml file in app/code/Jwd/WelcomeWorld/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="Jwd_WelcomeWorld" setup_version="1.0.0"> </module> </config>
Step2: Create app/code/Jwd/WelcomeWorld/registration.php
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Jwd_WelcomeWorld', __DIR__ ); ?>
Step3: Create a frontend router in app/code/Jwd/WelcomeWorld/etc/frontend/routes.xml
<?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="welcomeworld" frontName="welcomeworld"> <module name="Jwd_WelcomeWorld"/> </route> </router> </config>
Step4: Create the file index.php for controller action in app/code/Jwd/WelcomeWorld/Controller/Index. This will map to http://127.0.0.1/magento2/welcomeworld/index/index
welcomeworld: front name
index: name of controller folder
index: name of action file – index.php
Each action is its own class extending \Magento\Framework\App\Action\Action. In every action file, there will be a method name excute() that will involked when the action is called
<?php namespace Jwd\WelcomeWorld\Controller\Index; class Index extends \Magento\Framework\App\Action\Action { /** @var \Magento\Framework\View\Result\Page */ protected $resultPageFactory; /** * @param \Magento\Framework\App\Action\Context $context */ public function __construct(\Magento\Framework\App\Action\Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory) { $this->resultPageFactory = $resultPageFactory; parent::__construct($context); } /** * @return \Magento\Framework\View\Result\PageFactory */ public function execute() { $resultPage = $this->resultPageFactory->create(); $resultPage->getConfig()->getTitle()->prepend(__('Jwd WelcomeWorld')); return $resultPage; } } ?>
Step5: Create a layout file in the following directory app\code\Jwd\WelcomeWorld\View\frontend\layout\helloworld_index_index.xml
<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceContainer name="content"> <block class="Jwd\WelcomeWorld\Block\WelcomeWorld" name="lofformbuilder-toplinks" template="Jwd_WelcomeWorld::welcomeworld.phtml"/> </referenceContainer> </body> </page>
Step6: Create a block for our module. Create block file app/code/Jwd/WelcomeWorld/Block/WelcomeWorld.php
<?php namespace Jwd\WelcomeWorld\Block; class WelcomeWorld extends \Magento\Framework\View\Element\Template { public function _prepareLayout() { return parent::_prepareLayout(); } } ?>
Step7: Create a template file app/code/Jwd/WelcomeWorld/View/frontend/templates/welcomeworld.phtml
Welcome to Magento 2
Step 8: Active Jwd_WelcomeWorld extension
We have two ways to active Ves_Helloworld extension
1. Directly edit file app/etc/config.xml: In the array module, add the element: ‘Jwd_WelcomeWorld’ => 1
<?php return array ( 'modules' => array ( 'Jwd_WelcomeWorld' => 1, 'Magento_Store' => 1, 'Magento_Directory' => 1, 'Magento_Backend' => 1, 'Magento_Backup' => 1, 'Magento_Theme' => 1, 'Magento_BundleImportExport' => 1, 'Magento_CacheInvalidate' => 1, 'Magento_Eav' => 1, 'Magento_Customer' => 1, 'Magento_CatalogImportExport' => 1, 'Magento_Indexer' => 1, 'Magento_Cms' => 1, 'Magento_Search' => 1, 'Magento_Catalog' => 1, 'Magento_Rule' => 1, 'Magento_CatalogInventory' => 1, 'Magento_CheckoutAgreements' => 1, 'Magento_Payment' => 1, 'Magento_CmsUrlRewrite' => 1, 'Magento_Config' => 1, 'Magento_ConfigurableImportExport' => 1, 'Magento_Msrp' => 1, 'Magento_Contact' => 1, 'Magento_Cookie' => 1, 'Magento_Cron' => 1, 'Magento_Widget' => 1, 'Magento_Bundle' => 1, 'Magento_CustomerImportExport' => 1, 'Magento_DesignEditor' => 1, 'Magento_Developer' => 1, 'Magento_Dhl' => 1, 'Magento_Authorization' => 1, 'Magento_Downloadable' => 1, 'Magento_Quote' => 1, 'Magento_Email' => 1, 'Magento_Fedex' => 1, 'Magento_SalesSequence' => 1, 'Magento_Sales' => 1, 'Magento_GoogleAnalytics' => 1, 'Magento_GoogleOptimizer' => 1, 'Magento_GoogleShopping' => 1, 'Magento_GroupedImportExport' => 1, 'Magento_GroupedProduct' => 1, 'Magento_ImportExport' => 1, 'Magento_CatalogRule' => 1, 'Magento_User' => 1, 'Magento_LayeredNavigation' => 1, 'Magento_Log' => 1, 'Magento_MediaStorage' => 1, 'Magento_Checkout' => 1, 'Magento_Multishipping' => 1, 'Magento_Newsletter' => 1, 'Magento_OfflinePayments' => 1, 'Magento_SalesRule' => 1, 'Magento_PageCache' => 1, 'Magento_Captcha' => 1, 'Magento_Persistent' => 1, 'Magento_ProductAlert' => 1, 'Magento_GoogleAdwords' => 1, 'Magento_Reports' => 1, 'Magento_RequireJs' => 1, 'Magento_Review' => 1, 'Magento_Rss' => 1, 'Magento_CatalogWidget' => 1, 'Magento_GiftMessage' => 1, 'Magento_OfflineShipping' => 1, 'Magento_ConfigurableProduct' => 1, 'Magento_CatalogSearch' => 1, 'Magento_SendFriend' => 1, 'Magento_Shipping' => 1, 'Magento_Sitemap' => 1, 'Magento_AdminNotification' => 1, 'Magento_Tax' => 1, 'Magento_TaxImportExport' => 1, 'Magento_CatalogUrlRewrite' => 1, 'Magento_Translation' => 1, 'Magento_Ui' => 1, 'Magento_Ups' => 1, 'Magento_UrlRewrite' => 1, 'Magento_Integration' => 1, 'Magento_Usps' => 1, 'Magento_Variable' => 1, 'Magento_Version' => 1, 'Magento_Webapi' => 1, 'Magento_Weee' => 1, 'Magento_CurrencySymbol' => 1, 'Magento_Wishlist' => 1, ), ); ?>
Open Command line in folder root of magento and run commands: php bin/magento setup:upgrade
Run URL at frontend: http://127.0.0.1/magento2/welcomeworld/index/index
Magento Development | PSD To Magento | Magento Theme Development | Magento Development Services