Introduction: How to Create a Simple Module With Magento 2.0?
Magento 2.0 has been the talk for some time now. Developers are working through its functionalities, and creating example versions to understand the framework. Here you will learn how to create a simple module using this platform.
Declare the Module
The module that you are creating is Magento_Hello, an example simple module. Before you do anything, you will need to declare the module. Begin by writing the file module.xml in the path app/code/magento/hello/etc/module.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Magento_Hello" schema_version="0.0.1"/>
</config>
You will need to write the above mentioned code along the path mentioned to declare the module.
Configure the Module
Your next step is to configure the module, Magento_Hello that you have just created. You will need to create a controller and an action.
Create a file index.php along the following path
app/code/Magento/Hello/Controller/Index/Index.php
index, the folder is the controller while the file index.php is the action in this case. You will need to use execute () function to control the action
Here's the code that will trigger the action for this module
namespace Magento\Hello\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action
{
public function execute()
{
$this->_view->loadLayout();
$this->_view->getLayout()->initMessages();
$this->_view->renderLayout();
}
}
Create a Block
You will need to create a block in order to configure the module. Go to
app/code/Magento/Hello/Block/Hello.php
namespace Magento\Hello\Block;
Paste the following code to this path
class Hello extends \Magento\Framework\View\Element\Template
{
public function _prepareLayout()
{
return parent::_prepareLayout();
}
}
Write the Configuration File
Here, config.xml will configure the default configuration within tag<default>
The frontend router information will be placed in the following path
Magento/Hello/etc/frontend/routes.xml
The front end event will be declared along the following path
Magento/Hello/ect/frontend/events.xml
As mentioned the routers are declared using the following code in
Magento/Hello/etc/frontend/routes.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
<router id="standard">
<route id="hello" frontName="hello">
<module name="Magento_Hello" />
</route>
</router>
</config>
Create Frontend Template
You will need to write the layout file to the following path
app\code\Magento\Hello\view\frontend\layout\hello_index_index.xml
The layout file uses the nomenclature based on its structure
router name_controlle namer_action name
Here's the code for the layout file
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Magento\Hello\Block\Hello" name="hello" template="success.phtml">
</block>
</referenceContainer>
</body>
</page>
Now, create a file success.phtml to the following path
app\code\Magento\Hello\view\frontend\templates\success.phtml
<?php echo ‘Successful! This is a simple module in Magento 2.0′; ?>
This will act as the reporting file, which will announce the successful creation of a module
Activate the Module
Finally, you will need to activate the module by opening the config file app/etc/config.xml
In the array module, add the element Magento_Hello=>1
You will need to run the following link to see your module live
http://localhost/magento20/hello/index/index
Deepa, a technical writer with Semaphore Software, who now devotes her time in advising its clients to hire offshore magento developers. Her love for reading helps her constantly provide latest information on different technical and design aspects of Magento


