Quantcast
Channel: Magento 2 – DCKAP
Viewing all articles
Browse latest Browse all 135

HOW TO CREATE A MODULE IN MAGENTO 2 ?

$
0
0

This blog post is part of the ongoing series of Magento 2 articles that we have been concentrating for the past few weeks.  Let’s have a look at creating modules in Magento 2.

To create the module, lets understand the differences in directory structure between the two versions of Magento. In Magento 2, code pools are eliminated from the code-base file structure, now all the modules are grouped by vendor. We can now directly create the modules inside the app/code directory.

Magento 1.x
Magento 2.x
/app/code
|_/community
|_/core
|_/Enterprise
|_/
|_/
|_...
|_/Mage
|_/
|_/
|_/Zend
/app/code
|_/Magento
|_/
|_/
|_...
|_/Vendor1
|_/Vendor2
|_...

Now, let’s understand the steps involved in creating a simple Welcome module with namespace Dckap.

Step 1: We need to create a module.xml to declare a module in our Magento store.

Create the file name called module.xml under app/code/Dckap/Welcome/etc/module.xml

<?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="Dckap_Welcome" schema_version="0.0.1" active="true"/>
</config>

Step 2: We need to enable our module by editing the config.xml file which can be found in app/etc/config.xml directory. Add ‘Dckap_Welcome’=>1

Please refer the below screen shot.

In line no 124 I have added ‘Dckap_Welcome’=>1 to enable our module.

magento 2

Step 3: Next, we need to create a controller file.

Create a file name called index.php in the following directory app/code/Dckap/Welcome/Controller/Index/Index.php

Here the folder Index is name of the controller and index.php is an action file.

<?php
namespace \Welcome\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action
{
    public function execute()
    {
        $this->_view->loadLayout();
        $this->_view->getLayout()->initMessages();
        $this->_view->renderLayout();
    }
}

 Step 4: Lets create a block for our module. Create a block called Welcome.php in below mentioned directory.

 app/code/Dckap/Welcome/Block/Welcome.php

<?php
namespace \Welcome\Block;
class Welcome extends \Magento\Framework\View\Element\Template
{
    public function _prepareLayout()
    {
        return parent::_prepareLayout();
    }
}

Step 5: Once the block is completed, create a front end router file by adding an xml file, “routes.xml” in the following directory.

app/code/Dckap/Welcome/etc/frontend/routes.xml

<?xml version="1.0"?>
<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="welcome" frontName="welcome">
            <module name="Dckap_Welcome" />
        </route>
    </router>
</config>

Step 6: Create a template file for the module. app/code/Dckap/Welcome/view/frontend/templates/welcome.phtml

<strike> Magento 1</strike> Magento 2
<h1> Welcome to Magento 2 </h2>
<?php echo 'This is the Simple Welcome Module in Magento 2.0';?>

Step 7: In the final step we will create a layout in the following directory.

app/code/Dckap/Welcome/view/frontend/layout/welcome_index_index.xml

In Magento 2, we need to create the xml file in the above format, router name followed by the controller name and then the action name.

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <head>
        <title>Welcome to Magento World</title>
    </head>
    <body>
        <referenceContainer name="content">
            <block name="welcome" template="welcome.phtml">
            </block>
        </referenceContainer>
    </body>
</page>

Once we have completed all the steps, we can now access the new welcome module.

magento 2 modules

This is just a quick way to create a simple welcome module in Magento 2. Based on these basic techniques we can create customized modules according to our business needs. As we explore more into Magento 2, in the following weeks we shall have an in depth analysis on the other features. Follow us on Twitter or LinkedIn to see our next post.

The post HOW TO CREATE A MODULE IN MAGENTO 2 ? appeared first on .


Viewing all articles
Browse latest Browse all 135

Trending Articles