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

&lt;?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>
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *

© Copyright 2017 - Magento Services | Magento Development Comapny
Request Free Quotation

We are glad that you preferred to contact us. Please fill our short form and one of our friendly team members will contact you back.

X
CONTACT US