How To Create a Magento 2 Module

In this article we'll try to answer the first question of every beginner, how to create module in magento 2. To answer their questions we'll create one HelloWorld extension and learn how to create Magento 2 Extension.

Let's get started!!

To Create the module you have to follow the simple step which are listed the below for create Magento 2 HelloWorld Module.

Before we start, we need to create Devhooks folder as a Namespace at app/code/ and create a Module folder HelloWorld at app/code/Devhooks/ location.

Step 1: Create a module.xml file at app/code/Devhooks/HelloWorld/etc/ and paste the below code.

Note: Don't forgot to replace the Devhooks_HelloWorld with your Namespace and Module Name if it's different.

saveCopyzoom_out_map
<?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="Devhooks_HelloWorld" schema_version="0.0.1" setup_version="0.0.1"> </module> </config>

If your module depends on any other module then add it's dependencies under <module> tag in module.xml file. Check below code for your reference.

saveCopyzoom_out_map
<module name="Devhooks_HelloWorld" schema_version="0.0.1" setup_version="0.0.1"> <sequence> <module name="Magento_Customer"/> <module name="Magento_Catalog"/> </sequence> </module>

Step 2: Create registration file registration.php at app/code/Devhooks/HelloWorld/ and paste the below code.

saveCopyzoom_out_map
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Devhooks_HelloWorld', __DIR__ );

Step 3: Create a frontend router routes.xml at app/code/Devhooks/HelloWorld/etc/frontend/ and paste the below code.

saveCopyzoom_out_map
<?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="helloworld" frontName="helloworld"> <module name="Devhooks_HelloWorld"/> </route> </router> </config>
  • In Above code <router id="standard"> indicates which node Magento should look at to find the URL's front Name.

  • Then, the route id="helloworld" shown which router we will use (frontend or adminhtml).

  • And route frontName="helloworld" is the first part of the URL and it should be unique.

Step 4: Create a Controller file Index.php at app/code/Devhooks/HelloWorld/Controller/Index/ location and paste the below code.

saveCopyzoom_out_map
<?php namespace Devhooks\HelloWorld\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); } /** * Blog Index, shows a list of recent blog posts. * * @return \Magento\Framework\View\Result\PageFactory */ public function execute() { $resultPage = $this->resultPageFactory->create(); $resultPage->getConfig()->getTitle()->prepend(__('Devhooks Page Custom Title')); return $resultPage; } }

In above controller action we have extend magento default action \Magento\Framework\App\Action\Action to use some default methods. In every action file, there will be a method name execute() that will invoked when the action is called. Also, we've defined the page title. You can change the code as per your need.

This controller will map to http://domainname.com/helloworld/index/index URL.

Now, let's see how to find the action of any module using URL. For Ex URL is http://domainname.com/helloworld/index/index

So, in above URL

  • helloworld: front name
  • index: name of controller folder
  • index: name of action file – index.php

Step 5: Create a layout file helloworld_index_index.xml at app/code/Devhooks/Helloworld/view/frontend/layout/ location and paste the below code.

saveCopyzoom_out_map
<?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="Devhooks\HelloWorld\Block\HelloWorld" name="formbuilder" template="Devhooks_HelloWorld::helloworld.phtml"/> </referenceContainer> </body> </page>

The layout file name should be defined based on route_controller_action.

Step 6: Create block file HelloWorld.php at app/code/Devhooks/HelloWorld/Block/ location and paste the below code.

saveCopyzoom_out_map
<?php namespace Devhooks\HelloWorld\Block; class HelloWorld extends \Magento\Framework\View\Element\Template { public function _prepareLayout() { return parent::_prepareLayout(); } public function getSampleText() { return 'Hello World'; } }

Step 7: Create a template file helloworld.phtml at app/code/Devhooks/HelloWorld/view/frontend/templates/ location and paste the below code.

saveCopyzoom_out_map
<h3>This is <?php echo $this->getSampleText(); ?> extension output.</h3>

Step 8: Now it's time to active Devhooks_HelloWorld extension.

For that we've a two ways to active Devhooks_Helloworld extension.

1. Directly edit app/etc/config.php file and add the following line into the array: 'Devhooks_Helloworld' => 1

2. Open Command line in folder root of magento and run the below commands.

saveCopyzoom_out_map
php bin/magento setup:upgrade php bin/magento cache:flush

Bingo!! we are done all steps.

Run the following link http://domainname.com/helloworld/index/index, and check the result.

That's it!!

I hope this article helped you to find what you were looking for.

Bookmark it for your future reference. Do comment below if you have any other questions on that.

P.S. Do share this article with your team.

Review other articles maybe it'll help you too.




Recent Articles
Tags
Newsletter
Chrome Extension
Copyright © 2024 devhooks.in All rights reserved.
Ads OFF toggle_off
wifi_off