How to create a Magento 2 extension

Magewallet | Magento 2 Extensions, Themes & Solutions

How to create a Magento 2 extension

Happy to see you guys again!

In this blog post, we will learn how to create Magento 2 extension step by step and basic knowledge required to setup a module. we will also introduce the coding of Magento 2 in the form of a “Hello Developer” style. This blog post also includes some basic functionality, covering as many development aspects as possible in this case.

Lets consider module with Namespace is Magewallet and Module Name is HelloDeveloper. Lets list down all the files require for this extension.

– app/code/Magewallet/HelloDeveloper/registration.php
– app/code/Magewallet/HelloDeveloper/composer.json
– app/code/Magewallet/HelloDeveloper/etc/module.xml
– app/code/Magewallet/HelloDeveloper/etc/frontend/routes.xml
– app/code/Magewallet/HelloDeveloper/Controller/Index/index.php
– app/code/Magewallet/Helloworld/View/frontend/layout/hellodeveloper_index_index.xml
– app/code/Magewallet/HelloDeveloper/Block/HelloDeveloper.php
– app/code/Magewallet/HelloDeveloper/View/frontend/templates/hellodeveloper.phtml

First we will create registration file at app/code/Magewallet/HelloDeveloper/registration.php file

Now, create a composer.json file in app/code/Magewallet/HelloDeveloper/ folder as per below.

now create module.xml file in app/code/Magewallet/HelloDeveloper/etc

Create a frontend router in app/code/Magewallet/HelloDeveloper/etc/frontend/routes.xml

here the router ID shown which router we will use either frontend or adminhtml. Pay attention that the front name is the first part of the URL and it should be unique.

Create a Controller action

Create the file index.php in app/code/Magewallet/HelloDeveloper/Controller/Index.

Create a layout file at app\code\Magewallet\Helloworld\View\frontend\layout\hellodeveloper_index_index.xml

Create block file app/code/Magewallet/HelloDeveloper/Block/HelloDeveloper.php

Create a template file app/code/Magewallet/HelloDeveloper/View/frontend/templates/hellodeveloper.phtml

Active Magewallet_HelloDeveloper extension

Open Command line in folder root of Magento and run commands

In order to cross check the routing and controller access below URL.

You have known all the steps to write a simple extension in Magento 2. if you have any questions about this steps of Magento 2 extension development, please ask them in comments.
Enjoy Magento 2! Enjoy Magewallet!!


1 Comment

Herder Sutkus

34 mins ago

Very nice post. can you please add zip file with the above code snippet? I really appreciate for your efforts!! Thanks Mahendra.

Leave Comment


Latest Tweets