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


Module Block

Module Controller

Module Configuration in etc folder

Module view files

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="" 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>

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



Now your Basic module is registered

Run basic commands to enable the module and run setup 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:
your module url depends on this
for example
our module url will be


<?xml version="1.0"?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
<router id="standard">
  <route id="tutorial" frontName="tutorial">
        <module name="Magentoservices_Tutorial" />

(d).Creating controller file
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


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;
	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

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/

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

<?xml version="1.0"?>
<page xmlns:xsi="" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd" layout="1column">
		<referenceContainer name="content">
		    <block class="Magentoservices\Tutorial\Block\Tutorial" name="Tutorial" template="tutorial.phtml" />

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

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

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