MyPage is a personalized page based on your interests.The page is customized to help you to find content that matters you the most.


I'm not curious

How to Create a Simple Module with Magento 2.0?

Published on 19 May 15
0
4
How to Create a Simple Module with Magento 2.0? - Image 1
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

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

Conclusion
Creating a simple module certainly differs from Magento 1.0, but follows similar steps. You will see that creating a module is quite easy and effortless. You just need to remember the structure and should give a valid nomenclature to the module right when you are creating it.
This blog is listed under Development & Implementations and E-Commerce Community

Related Posts:
Post a Comment

Please notify me the replies via email.

Important:
  • We hope the conversations that take place on MyTechLogy.com will be constructive and thought-provoking.
  • To ensure the quality of the discussion, our moderators may review/edit the comments for clarity and relevance.
  • Comments that are promotional, mean-spirited, or off-topic may be deleted per the moderators' judgment.
You may also be interested in
 
Awards & Accolades for MyTechLogy
Winner of
REDHERRING
Top 100 Asia
Finalist at SiTF Awards 2014 under the category Best Social & Community Product
Finalist at HR Vendor of the Year 2015 Awards under the category Best Learning Management System
Finalist at HR Vendor of the Year 2015 Awards under the category Best Talent Management Software
Hidden Image Url

Back to Top