Create a simple module in Magento2
I have created a module with Namespace as Rahul and Module Name as HelloWorld
Step1: Create a module.xml file in app/code/Rahul/HelloWorld/etc
<?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="Rahul_HelloWorld" setup_version="1.0.0">
</module>
</config>
Step2: Create app/code/Rahul/HelloWorld/registration.php file
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Rahul_HelloWorld',
__DIR__
);
Step3: Create a frontend router in app/code/Rahul/HelloWorld/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="helloworld" frontName="helloworld">
<module name="Rahul_HelloWorld"/>
</route>
</router>
</config>
The first section of the route string indicates which node Magento should look at to find the URL’s front Name.
Then, the router ID shown which router we will use: frontend or adminhtml(the same like in old Magento).
Step4: Create a Controller action
Create the file index.php in app/code/Rahul/HelloWorld/Controller/Index.
This will map to http://localhost/magento2/helloworld/index/index
helloworld: front name
index: name of controller folder
index: name of action file – index.php
Each action has 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 Rahul\HelloWorld\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action {
protected $resultPageFactory;
public function __construct(\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory) {
$this->resultPageFactory = $resultPageFactory;
parent::__construct($context);
}
public function execute()
{
$resultPage = $this->resultPageFactory->create();
$resultPage->getConfig()->getTitle()->prepend(__('Rahul HelloWorld'));
return $resultPage;
}
}
Step5: Create a layout file in the following directory app/code/Rahul/HelloWorld/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="Rahul\HelloWorld\Block\HelloWorld" name="lofformbuilder-toplinks" template="Rahul_HelloWorld::helloworld.phtml"/>
</referenceContainer>
</body>
</page>
Step6: Lets create a block for our module. Create block file app/code/Rahul/HelloWorld/Block/HelloWorld.php
<?php
namespace Rahul\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
public function _prepareLayout()
{
return parent::_prepareLayout();
}
}
Step7: Create a template file app/code/Rahul/HelloWorld/view/frontend/templates/helloworld.phtml
<h1 style="color: #f1703d;"> Welcome to Magento 2</h1>
Step 8: Active Rahul_HelloWorld extension
We have two ways to active Rahul_Helloworld extension
1. Directly edit file app/etc/config.xml: In the array module, add the element: ‘Rahul_Helloworld’ => 1
Rahul_helloworld_active
2. Open Command line in folder root of magento and run commands
php bin/magento setup:upgrade
You have known all the steps to write a simple module in Magento2. When you run the link:
http://localhost/magento2/helloworld/index/index, the result will be shown as the following
result on browser
Welcome to Magento 2
if you get the error as
Fatal error: Declaration of Rahul\HelloWorld\Controller\Index\Index::execute() must be compatible with Magento\Framework\App\ActionInterface::execute(Magento\Framework\App\RequestInterface $request) in D:\Xampp\htdocs\magento2dev\app\code\Rahul\HelloWorld\Controller\Index\Index.php on line 25
add permission to pub folder
chmod -R 777 var pub
Few Commands
upgrade: bin/magento setup:upgrade
permission: chmod -R 777 var pub
compile comand: bin/magento setup:di:compile
Step1: Create a module.xml file in app/code/Rahul/HelloWorld/etc
<?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="Rahul_HelloWorld" setup_version="1.0.0">
</module>
</config>
Step2: Create app/code/Rahul/HelloWorld/registration.php file
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Rahul_HelloWorld',
__DIR__
);
Step3: Create a frontend router in app/code/Rahul/HelloWorld/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="helloworld" frontName="helloworld">
<module name="Rahul_HelloWorld"/>
</route>
</router>
</config>
The first section of the route string indicates which node Magento should look at to find the URL’s front Name.
Then, the router ID shown which router we will use: frontend or adminhtml(the same like in old Magento).
Step4: Create a Controller action
Create the file index.php in app/code/Rahul/HelloWorld/Controller/Index.
This will map to http://localhost/magento2/helloworld/index/index
helloworld: front name
index: name of controller folder
index: name of action file – index.php
Each action has 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 Rahul\HelloWorld\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action {
protected $resultPageFactory;
public function __construct(\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory) {
$this->resultPageFactory = $resultPageFactory;
parent::__construct($context);
}
public function execute()
{
$resultPage = $this->resultPageFactory->create();
$resultPage->getConfig()->getTitle()->prepend(__('Rahul HelloWorld'));
return $resultPage;
}
}
Step5: Create a layout file in the following directory app/code/Rahul/HelloWorld/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="Rahul\HelloWorld\Block\HelloWorld" name="lofformbuilder-toplinks" template="Rahul_HelloWorld::helloworld.phtml"/>
</referenceContainer>
</body>
</page>
Step6: Lets create a block for our module. Create block file app/code/Rahul/HelloWorld/Block/HelloWorld.php
<?php
namespace Rahul\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
public function _prepareLayout()
{
return parent::_prepareLayout();
}
}
Step7: Create a template file app/code/Rahul/HelloWorld/view/frontend/templates/helloworld.phtml
<h1 style="color: #f1703d;"> Welcome to Magento 2</h1>
Step 8: Active Rahul_HelloWorld extension
We have two ways to active Rahul_Helloworld extension
1. Directly edit file app/etc/config.xml: In the array module, add the element: ‘Rahul_Helloworld’ => 1
Rahul_helloworld_active
2. Open Command line in folder root of magento and run commands
php bin/magento setup:upgrade
You have known all the steps to write a simple module in Magento2. When you run the link:
http://localhost/magento2/helloworld/index/index, the result will be shown as the following
result on browser
Welcome to Magento 2
if you get the error as
Fatal error: Declaration of Rahul\HelloWorld\Controller\Index\Index::execute() must be compatible with Magento\Framework\App\ActionInterface::execute(Magento\Framework\App\RequestInterface $request) in D:\Xampp\htdocs\magento2dev\app\code\Rahul\HelloWorld\Controller\Index\Index.php on line 25
add permission to pub folder
chmod -R 777 var pub
Few Commands
upgrade: bin/magento setup:upgrade
permission: chmod -R 777 var pub
compile comand: bin/magento setup:di:compile
Comments
Post a Comment