Monday , April 24 2017
Home >> All tutorial >> Magento tutorial for beginners: Creating A Simple Module in Magento 2.0

Magento tutorial for beginners: Creating A Simple Module in Magento 2.0

Magento tutorial for beginners understand that Magento 2 will be released in a near future, so it is necessary to develop modules for the new system. To solve this case, we give you this interesting Magento extension tutorial, which compare Magento 1 and Magento 2 in some features below, and give you a step-by-step tutorial about creating a simple module in Magento 2. Let’s start!

1. Differences between Magento 1.0 and Magento 2.0

This is a table which compare Magento 1 and Magento 2 in many features:

Magento 1.0 Magento 2.0
Folder app/code includes subfolders: core, community, local Folder app/code includes subfolders Magento and Zend. In Magento 1.0,  Magento andZend are subfolders of the folder core
Codes created by developers are located at app/code/local or app/code/community Codes created by developers are written directly in app/code. There is no difference between local and community
Module declaration file is a xml file in app/etc/modulesEg:  Module Checkout in Magento Core is declared in app/etc/modules/Mage_All.xml Module declaration file is always module.xml which is written directly to folder etc in the moduleEg:  module Checkout in Magento Core is declared in app/code/Magento/Checkout/etc/module.xml
Layout and template are saved in folder app/designEg:  app/design/frontend/default/default/layout Layout and template are saved in the folder “View” in the module. The folder is the same level with some folders like: Block, Controller, Helper, Model, etc. in the moduleEg:  app/code/Magento/Hello/view/frontend/layout

Above are some basic differences between Magento 1.0 and Magento 2.0 so that you can easily visualize the folder structure in Magento 2.0. Thus, making a simple module in Magento 2.0 is just a breeze. For deeper understand, move to the next part & practice.

2. Create a simple module in Magento 2.0

(Namespace: Magento, Module Name: Hello)

Example link on local host: http://localhost/magento20/hello/index/index

 

 

Step 1: Write the file module.xml in app/code/Magento/Hello/etc/module.xml to declare the module.

<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>

Magento 2.0 Tutorial: How to create a simple magento extension 1

 

 

Step 2:

– Create controller and action:

– Create the file Index.php inapp/code/Magento/Hello/Controller/Index/Index.php

Folder Index plays the role of controller, while Index.php is action. The executive function of action Index is execute()

namespace MagentoHelloControllerIndex;
class Index extends MagentoFrameworkAppActionAction
{
  public function execute()
{
     $this->_view->loadLayout();
       $this->_view->getLayout()->initMessages();
    $this->_view->renderLayout();
}
}

Magento Tutorial: How to create a simple magento extension 2

– Create a Block: app/code/Magento/Hello/Block/Hello.php

app/code/Magento/Hello/Block/Hello.php
namespace MagentoHelloBlock;
class Hello extends MagentoFrameworkViewElementTemplate
{
public function _prepareLayout()
{
    return parent::_prepareLayout();
}
}

Magento Tutorial: How to create a simple magento extension 3

– Write configuration file: /app/code/Magento/Hello/etc/frontend/routes.xml

– In Magento 1.0, every information about router, events of frontend and backend is declared in Magento/Hello/etc/config.xml. However, in Magento 2.0, file config.xml only configures the default configuration value in tag <default>

+) Information about router of frontend will be reported in:

Magento/Hello/etc/frontend/routes.xml (it is similar to backend)

+) Event of frontend will be declared in: Magento/Hello/ect/frontend/events.xml  (it is similar to backend)

In the scope of a simple module, we only declare routers 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>

magento tutorial

 

 

Step 3: Create a Frontend Template

– Write a layout file: appcodeMagentoHelloviewfrontendlayouthello_index_index.xml

Name of layout file is really important in this step. It will be named after the structure: router name_controlle namer_action name

<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="MagentoHelloBlockHello" name="hello" template="success.phtml">
        </block>
    </referenceContainer>
</body>
</page>

magento tutorial

Then, we create a file success.phtml as reporting in layout file:

appcodeMagentoHelloviewfrontendtemplatessuccess.phtml

 

  1. <?php echo Successful! This is a simple module in Magento 2.0′; ?>

Magento Tutorial: How to create a simple magento extension 5

 

Step 4: Activate Magento_Hello extension in configuration file

– Open the file app/etc/config.xml

– In the array ‘module’, add the element: ‘Magento_Hello’ => 1,

Magento Tutorial: How to create a simple magento extension 6

You have known all the steps to write a simple module in Magento 2.0. When you run the link: http://localhost/magento20/hello/index/index the result will be shown as the following:

Magento Tutorial: How to create a simple magento extension 8

That is magento tutorial  of how to create a simple module in Magento 2.0. Hope you guys like my tutorial.


Ready to foster your knowledge with MAGENTO 2 BASIC GUIDE

The new Order Success combo with Magento 2 Inventory Management, Magento 2 Order Fulfillment, Magento 2 Dropship, Magento 2 Barcode Management and Magento 2 Purchase Management

Is your site still a mess and Magento extensions are not enough to save your site? Save your site with Magento Web Development Services now

magento services


About Champ