We use cookies to make your experience better. To comply with the new e-Privacy directive, we need to ask for your consent to set the cookies. Learn more.
Magento 2: Hello World Module
Creating a "Hello World" module in Magento 2 involves several steps. Here’s a basic guide to get you started:
1. Create the Module Declaration File
Create aregistration.php file in app/code/Vendor/Module/:
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Vendor_Module',
__DIR__
);
2. Create the Module Configuration File
Create amodule.xml file in app/code/Vendor/Module/etc/:
<?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="Vendor_Module" setup_version="2.0.0">
</module>
</config>
3. Create a Basic Controller
Create aIndex.php file in app/code/Vendor/Module/Controller/Index/:
<?php
namespace Vendor\Module\Controller\Index;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
class Index extends Action
{
protected $_resultPageFactory;
public function __construct(
Context $context,
PageFactory $resultPageFactory
) {
$this->_resultPageFactory = $resultPageFactory;
parent::__construct($context);
}
public function execute()
{
$resultPage = $this->_resultPageFactory->create();
// Set title of page
$resultPage->getConfig()->getTitle()->set(__('Hello world'));
return $resultPage;
}
}
4. Create a Basic Block
Create aHello.php file in app/code/Vendor/Module/Block/:
<?php
namespace Vendor\Module\Block;
use Magento\Framework\View\Element\Template;
class Hello extends Template
{
/**
* @return $this
*/
protected function _prepareLayout()
{
return parent::_prepareLayout();
}
/**
* getContentForDisplay
* @return string
*/
public function getContentForDisplay()
{
return __("Print what you want");
}
}
5. Create a Route
Create aroutes.xml file in app/code/Vendor/Module/etc/frontend/:
<?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="hello" frontName="hello">
<module name="Vendor_Module" />
</route>
</router>
</config>
6. Create the Layout XML File
Create ahello_index_index.xml file in app/code/Vendor/Module/view/frontend/layout/:
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Vendor\Module\Block\Hello" name="hello" template="hello_world.phtml" />
</referenceContainer>
</body>
</page>
7. Create the Template phtml File
Create ahello_world.phtml file in app/code/Vendor/Module/view/frontend/templates/:
<h2><?php /** @var Vendor\Module\Block\Hello $block */
echo $block->getContentForDisplay();?></h2>
<p>sample text</p>
8. Run the following command
bin/magento setup:upgrade
9. Output
