Magento2: create custom module

Today we will learn to create custom module in magento2-

For installation and initial learning please visit our previous blogs of magento2.

The overall development we are doing on Ubuntu 14.4, local environment the base directory structure of my local is –

You can download full code from GIT below given URL –


Step 1.Initialization of your custom module –

First of all we will have to initialize our custom module to the list of modules with magento configuration, for which we will have to first create a /var/www/html/magento2/app/code/A2bizz/Blog/registration.php file and write below code –




if you hit your magento2 URL i.e. http://localhost/magento2/, now you will get below issue –

Cannot read contents from file "/var/www/html/magento2/app/code/A2bizz/Blog/etc/module.xml"

Now create module.xml on file /var/www/html/magento2/app/code/A2bizz/Blog/etc/module.xml and write below code –

<?xml version="1.0"?>
<config xmlns:xsi=""  xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="A2bizz_Blog" setup_version="2.0.0" />

Now we have initialised our custom module, To check that please check /var/www/html/magento2/app/etc/config.php where your created module will be listed down, if not then upgrade custom module.

Step 2. creating controller & routing –

Now Further create controller file /var/www/html/magento2/app/code/A2bizz/Blog/Controller/Index/Index.php


namespace A2bizz\Blog\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
	protected $resultPageFactory;

	public function __construct(
		\Magento\Framework\App\Action\Context $context,
		\Magento\Framework\View\Result\PageFactory $resultPageFactory
		$this->resultPageFactory = $resultPageFactory;
    public function execute()
        return $this->resultPageFactory->create();


and a routes.xml file on path /var/www/html/magento2/app/code/A2bizz/Blog/etc/frontend/routes.xml,which will provide the path execution to your custom module –

<?xml version="1.0"?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="blog" frontName="blog">
            <module name="A2bizz_Blog" />

here, in this xml we initializing the calling path for our custom module. In this way we have successfully hit out the controller for our custom module. Here in our example hit –

if the hitting above URL is showing blank white page, it means controller is hitted in a proper manner and now asking for the page layout.

Step 3. designing layout (view) for our custom module –

Now Further create layout files for your custom module /var/www/html/magento2/app/code/A2bizz/Blog/view/frontend/layout/blog_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <referenceContainer name="content">
            <block class="A2bizz\Blog\Block\Blog" name="blog" template="A2bizz_Blog::blog.phtml" />

and add /var/www/html/magento2/app/code/A2bizz/Blog/view/frontend/templates/blog.phtml

echo "Blog Template";

Now hit your custom module URL again, will show you below error
Object DOMDocument should be created.

it means it is unable to find corresponding Block method for its layout.

Now create your Block method, in /var/www/html/magento2/app/code/A2bizz/Blog/Block/Blog.php

namespace A2bizz\Blog\Block;

class Blog extends \Magento\Framework\View\Element\Template
	public function _prepareLayout()
	    return parent::_prepareLayout();

Now, Executing your URL http://localhost/magento2/blog, will show you complete module functionality.

Magento2: Module File Structure

Magento 2 Module File structure

In Magento2, there are lots of differences you will find out, due to security purposes. Magento2 looks for the files that make up a module, including configuration files, in particular places inside the module file structure. Follow the predefined file structure to ensure that your module works as expected.

A module can live or can be place now anywhere in Magento root directory. Regardless of where you add it, you must be registered, for Registering your module we need to implement registeration.php file.

A typical file structure for a Magento 2 module:




































Main Directories

The main directories are:

  • Block — it contains PHP classes as part of MVC vertical implementation of module logic.
  • Controller — it contains PHP classes as part of MVC vertical implementation of module logic.
  • Helper — it contains PHP classes as part of MVC vertical implementation of module logic.
  • Model — it contains PHP classes as part of MVC vertical implementation of module logic.
  • Setup — it contains classes for module database structure and data setup which are invoked when installing or upgrading.

Additional directories

Additionally, there are folders for configuration and other ancillary functions for items like plug-ins, internationalization, and front-end layout files.

  • Api— it contains any PHP classes exposed to the API.
  • etc — it contains configuration files.
  • i18n — it contains localization different files, like language files.
  • Plugin — it contains any needed plugin files.
  • view — it contains non-object-oriented and non-static view level parts of the module such as design templates, email templates, and layout files.