Now Magento 2 has been launched , which has been completely revamped from what we had in Magento 1.x. Here we have displayed step by step process to create new Magento 2 module.
TW is the namespace and Hello is the module name
Step1: we need create a module.xml file in app/code/TW/Hello/etc
<?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="TW_Hello" setup_version="1.0.0">
</module>
</config>
Step2: Create app/code/TW/Hello/registration.php file
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'TW_Hello',
__DIR__
);
Step3: Create a frontend router in app/code/TW/Hello/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="hello" frontName="hello">
<module name="TW_Hello" />
</route>
</router>
</config>
Step4: Create a Controller action
Create the file index.php in app/code/TW/Hello/Controller/Index.
The above controller can be accessed from the browser http://<domainname>/hello/index/index
Each action has its own file, there will be a method name excute() that will invoked when the action is called.
<?php
namespace TW\Hello\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action {
/** @var \Magento\Framework\View\Result\Page */
protected $resultPageFactory;
/** * @param \Magento\Framework\App\Action\Context $context */
public function __construct(\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory) {
$this->resultPageFactory = $resultPageFactory;
parent::__construct($context);
}
/**
*
*
* @return \Magento\Framework\View\Result\PageFactory
*/
public function execute()
{
$resultPage = $this->resultPageFactory->create();
$resultPage->getConfig()->getTitle()->prepend(__('Hello Treewalker'));
return $resultPage;
}
}
Step5: Create a layout file in the following directory app\code\TW\Hello\View\frontend\layout\hello_index_index.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="TW\Hello\Block\HelloWorld" name="hello" template="helloworld.phtml">
</block>
</referenceContainer>
</body>
</page>
Step6: We will create block for our module. Create block file app/code/TW/Hello/Block/HelloWorld.php
<?php
namespace TW\Hello\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
public function _prepareLayout()
{
return parent::_prepareLayout();
}
public function sayHello(){
}
}
Step7: Create a template file app/code/TW/Hello/View/frontend/templates/helloworld.phtml
<h1> Welcome </h2>
<p><?php echo __('You are visiting the page of hello world'); ?></p>
Step8: Create a helper at this path : app/code/TW/Hello/Helper/Data.php
namespace TW\Hello\Helper;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
/**
* @var \Magento\Framework\App\Config\ScopeConfigInterfac
*/
protected $_scopeConfig;
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
) {
parent::__construct($context);
$this->_scopeConfig = $scopeConfig;
}
}
Step9: If you have data manipulation and interaction with the database you need to create the model and custom tables .
Create a schema installation script at path to create custom tables app/code/TW/Hello/Setup/InstallSchema.php
namespace TW\Hello\Setup;
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
class InstallSchema implements InstallSchemaInterface
{
public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$installer = $setup;
$installer->startSetup();
$table = $installer->getConnection()
->newTable($installer->getTable('tw_hello'))
->addColumn(
'id',
\Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
null,
['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
'Id'
)
->addColumn(
'label',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
null,
['default' => null, 'nullable' => false],
'Name'
)
->addColumn(
'value',
\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
null,
['default' => null, 'nullable' => false],
'Stores'
);
$installer->getConnection()->createTable($table);
$installer->endSetup();
}
}
Model Class
we need to create three classes to work with the model: the model itself, the resource model and the collection.
Let’s create a TW\Hello\Model\Hello.php file and use the following initialization:
namespace TW\Hello\Model;
class Hello extends \Magento\Framework\Model\AbstractModel
{
/**
* Model Initialization
*
* @return void
*/
protected function _construct()
{
$this->_init('tw_hello', 'id');
}
}
The resource model and the collection are stored in the following path:
TW\Hello\Model\ResourceModel\Hello.php
namespace TW\Hello\Model\ResourceModel;
class Hello extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
/**
* Model Initialization
*
* @return void
*/
protected function _construct()
{
$this->_init('tw_hello', 'id');
}
}
TW\Hello\Model\ResourceModel\Hello\Collection.php
namespace TW\Hello\Model\ResourceModel\Hello;
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
/**
* Define resource model
*
* @return void
*/
protected function _construct()
{
$this->_init('TW\Hello\Model\Hello', 'TW\Hello\Model\ResourceModel\Hello');
}
}
Step 9: Active TW_Hello extension
Directly edit file app/etc/config.xml: In the array module, add the element: ‘TW_Hello’ => 1
OR
Open Command line in folder root of magento and run commands
php bin/magento setup:upgrade
To see the the working module you can run the below link :
http://<domain_name>/hello/index/index