Wednesday, 6 August 2014

Creating a New Custom Module in Magento2

As we know Magento2 has differently structured in respect to Magento 1.x
So creating  new custom module is also need different approach.
This tutorials snippet is tested on Magento 2.0.0 dev83

As we know, in Magento 1.x module declaration file goes into app/etc/modules/ *.xml
But in Magento2 the structure has changed. Now we have to put it under the Module itself.

So here is the step to create a Hello world module.
Here we using 'Magentozend' as packagename  so you have to change it with Your Packagename whenever you like to copy it in your installation if you like.

Step 1: Create some folder
Create some folder  structure under the app/code. Are you looking for local? There is no local in magento2. You can develop your custom module under your namespace.

app/code/Magentozend/Hello
app/code/Magentozend/Hello/etc
app/code/Magentozend/Hello/etc/frontend
app/code/Magentozend/Hello/Controllers
app/code/Magentozend/Hello/view/frontend/layout
app/code/Magentozend/Hello/view/frontend/templates
app/code/Magentozend/Hello/Block

-- etc directory for storing configuration files.
-- Here "Magentozend" is Namespace or Packagename and "Hello" is Module name.
-- Controllers directory for putting all controller files.
-- Block directory for putting block files
-- view is for putting view files of frontend as well as adminhtml


Step 2: Creating configuration file.
Create a file app/code/Magentozend/Hello/etc/module.xml as configuration file. It contains module declaration and dependent module description.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/Magento/Framework/Module/etc/module.xsd">
    <module name="Magentozend_Hello" version="0.0.1" active="true">
        <sequence>
            <module name="Magento_Core"/>
        </sequence>
    </module>
</config>

Step 2: Creating frontend route file.
Create a file app/code/Magentozend/Hello/etc/frontend/routes.xml as frontend route file. It contains module routing details for frontend.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/Magento/Framework/App/etc/routes.xsd">
    <router id="standard">
        <route id="hello" frontName="hello">
            <module name="Magentozend_Hello" />
        </route>
    </router>
</config>

Step 3: Creating Block
Create file app/code/Magentozend/Hello/Block/Hello.php as block file.

<?php
namespace Magentozend\Hello\Block;
class Hello extends \Magento\Framework\View\Element\Template
{

}

Step 4: Creating Controller
Create a file app/code/Magentozend/Hello/Controllers/Index.php
The controller request falls to hello_index_index

<?php
namespace Magentozend\Hello\Controller;
class Index extends \Magento\Framework\App\Action\Action
{
    public function indexAction()
    {
        $this->_view->loadLayout();
        $this->_view->renderLayout();
    }
}


Step 4: Creating Layout file

Create a file app/code/Magentozend/Hello/view/frontend/layout/hello_index_index.xml
Here you can notice that the layout filename replicate as the handle name.

<?xml version="1.0"?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="../../../../../../../app/code/Magento/Core/etc/layout_single.xsd">
<update handle="page_one_column" />
    <referenceBlock name="page.main.title">
        <action method="setPageTitle">
            <argument translate="true" name="title" xsi:type="string">Title: Hello World</argument>
        </action>
    </referenceBlock>
    <referenceContainer name="content">
        <block class="Magentozend\Hello\Block\Hello" name="hello.world" template="hello.phtml"/>
    </referenceContainer>
</layout>


Step 5: Creating Template file

Create a file app/code/Magentozend/Hello/view/frontend/templates/hello.phtml

<h1><strong>Hello World</strong></h1>
<strong>Cool! Welcome to Magento2 Layouts Views </strong>


Step 6: Remove Cache
Do not forget to remove Cache if it is enabled and thats all.
You can get the module running by url http://sitename.com/hello or http://sitename.com/index.php/hello

Enjoy!!

You can get the code directly from my github account

https://github.com/krajiv26/magento2HelloModule

No comments :

Post a Comment