Magento 2 Module Development – Step By Step Guide

Home >> Blog >> Magento 2 >> Magento 2 Module Development – Step By Step Guide
Magento 2 Module Development - Step By Step Guide

What is a Magento Extension?

An extension is a logical group – that is, a directory containing blocks, controllers, helpers, models – that are related to a specific business feature. In keeping with Magento’s commitment to optimal modularity
, a module encapsulates one feature and has minimal dependencies on other modules.

Extensionsand themes are the units of customization in Magento. Modules provide business features, with supporting logic, while themes strongly influence user experience and storefront appearance.

Both components have a life cycle that allows them to be installed, deleted, and disabled. From the perspective of both merchants and extension developers, modules are the central unit of Magento organization.

The Magento Framework provides a set of core logic: PHP code, libraries, and the basic functions that are inherited by the modules and other components.

You must follow a PSR compliant structure when building a module.

In this guide we will help you create a simple Magento 2 extension which can be useful as a base to create more advanced extensions for your Magento 2 webshop

Follow below steps for Magento 2 extension development:

Disable Cache

Make sure that Magento cache is disabled so that you don’t have to clear the cache while making changes in the extension. This will save you a lot of time in development.

You can disable the cache from Admin -> System -> Cache Management -> select all types and disable.

Developer Mode

Ensure that your Magento store is in developer mode so that all changes are reflected in real time and you can view any errors you come across while doing coding.

For this you have to run commands from terminal using SSH access. Login to your Magento store SSH account then in Magento root directory run below command:

php bin/magento deploy:mode:set developer

Module Initialization

You may already have experience with this if you are Magento 1 developer and have used its terminology before for code pools. Magento 2 framework is same but doesn’t have extra code pools. They are grouped with namespaces and have to reside in app/code directory.

We have to first create files and directories to initialize and register the extension. Create below directories:


After that create a file named module.xml in directory app/code/Icecube/Firstextension/etc and place following code:


Next we need to register your module by creating file registration.php in directory app/code/Icecube/Firstextension with and inserting code:


By using route with id “firstextension” we have now defined the path for new frontend router.

We will use frontName attribute as first part of the URL.

Magento 2 has the URL’s constructed as below:


Our extension’s final URL becomes:


After the route has been defined we need to create the landing controller file index.php in directory app/code/Icecube/Firstextension/Controller/Index with following code:


We need to create a layout handle for each page as each controller action has layout handle and in our case the layout handle is firstextension_index_index.

Now we have to create a phtml file to set the template of our block which we have added in the layout handle. Create firstextension.phtml in directory app/code/Icecube/Firstextension/view/frontend/templates and place following code:


By using $this object variable, we have referenced the block class we have created and from it we have called the method we have defined getFirstExtensionTxt() which returns the text ‘This is your first extension!’


Last but not the least, we have to run one command to deploy the files we have created for extension so that its usable on Magento store. Login to your Magento store SSH account and go to Magento root directory and run below command:

php bin/magento setup:upgrade

You can make sure that your extension is installed properly by going to Admin → Stores → Configuration → Advanced → Advanced and check that the module is present in the list otherwiseyou have to open app/etc/config.php and check the array for the ‘Icecube_Firstextension’ key, whose value should be set to 1.

And that’s all folks. Go to your browser and open the URL and you should get the text displayed on your store page.

If you face any other technical issues while building your magento 2 extension, Please feel free to contact us and out Magento 2 developers would be glad to assist.

Leave a Reply

Blog Categories
eCommerce Services
Recent Posts
Get Free Quote Now