How to create module in Magento 2

  • May 15, 2017
No Comments

Magento 2 is a module based e commerce system . and you can make custom feature like admin controller , product customization , custom menu , custom design by module . Follow easy steps to create module in Magento 2

(I). Create your module basic folder structure

app/code/Magentoservices
app/code/Magentoservices/Tutorial

Module Block
app/code/Magentoservices/Tutorial/Block

Module Controller
app/code/Magentoservices/Tutorial/Controller

Module Configuration in etc folder
app/code/Magentoservices/Tutorial/etc
app/code/Magentoservices/Tutorial/etc/frontend

Module view files
app/code/Magentoservices/Tutorial/view
app/code/Magentoservices/Tutorial/view/layout
app/code/Magentoservices/Tutorial/view/templates

you can use your namespace  and module name at the place of Magentoservices(namespace) and Tutorial(modulename)

(II). First  create module.xml inside etc folder

(a) app/code/Magentoservices/Tutorial/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="Magentoservices_Tutorial" setup_version="1.0.0" schema_version="1.0.0"></module>
</config>

(b).To register the module, create a registration.php file inside app/code/Magentoservices/Tutorial/

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
	'Magentoservices_Tutorial',
		__DIR__
	);

Now your Basic module is registered

Run basic commands to enable the module and run setup upgrade.you have to login to SSH and navigate to root directory and the run below commands

php bin/magento module:enable Magentoservices_Tutorial

php bin/magento setup:upgrade

(c). Create your module Controller router file
open app/code/Magentoservices/Tutorial/etc/frontend and create routes.xml for frontend controller and for admin controller create app/code/Magentoservices/Tutorial/etc/adminhtml
folder and the inside app/code/Magentoservices/Tutorial/etc/adminhtml create routes.xml

Route’s in magento are divided into 3 parts:
Route_id
controller
action
your module url depends on this
for example http://Tutorial.com/index.php/route_id/controller/action
our module url will be http://Tutorial.com/index.php/tutorial/index

 app/code/Magentoservices/Tutorial/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="tutorial" frontName="tutorial">
        <module name="Magentoservices_Tutorial" />
   </route>
</router>
</config>

(d).Creating controller file
app/code/Magentoservices/Tutorial/Controller/Index.php
create Index.php file inside app/code/Magentoservices/Tutorial/Controller/Index/
for admin controller you can to create Adminhtml folder app/code/Magentoservices/Tutorial/Controller/Adminhtml

<?php

namespace Magentoservices\Tutorial\Controller\Index;
use Magento\Framework\App\Action\Context;
class Index extends \Magento\Framework\App\Action\Action{
	protected $_resultPageFactory;

    public function __construct(Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory)
	{
		$this->_resultPageFactory = $resultPageFactory;
		parent::__construct($context);
	}
	public function execute()
		{
		$resultPage = $this->_resultPageFactory->create();
		return $resultPage;
		}
}

(e). Creating a block  for your module
Create a tutorial.php file in the app/code/Magentoservices/Tutorial/Block folder with the following

<?php
namespace Magentoservices\Tutorial\Block;
class Tutorial extends \Magento\Framework\View\Element\Template{
	public function getCustomcontent()
		{
			$content = "This is custom content of block";
			return $content;
		}
	}

(f).Now last layout and Template files are left so create them and your first modulee is ready
Create layout and templates folder inside app/code/Magentoservices/Tutorial/view/

app/code/Magentoservices/Tutorial/view/frontend/layout
app/code/Magentoservices/Tutorial/view/frontend/templates
Create tutorial_index_index.xml inside app/code/Magentoservices/Tutorial/view/frontend/layout/

<?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" layout="1column">
	<body>
		<referenceContainer name="content">
		    <block class="Magentoservices\Tutorial\Block\Tutorial" name="Tutorial" template="tutorial.phtml" />
		</referenceContainer>
	</body>
</page>

Create template file inside app/code/Magentoservices/Tutorial/view/frontend/templates/tutorial.phtml

<div class="customcontent">
<h1><?php echo $this->getCustomcontent(); ?></h1>
</div>

we are expert in create module Magento 2 so feel free to contact us 

About us and this blog

We are a digital magento 2  development agency with a focus on helping our customers achieve great results across several key areas.

More From Our Blogs

See all posts

Leave a Comment