How to Create Module in Magento 2

I’m starting my guide for development series in Magento 2, let’s start this guide with “HellowWorld, Module Development in Magento 2”.

In this guide we will discuss and learn complete guide about “How to Create hellowWorld Module in Magento 2”.
NOTE: The process of creating module in Magento 1 and Magento 2 is different and the concepts of code pool like core/community and local only existed in Magento 1 and you will not be using it for Magento 2.

Things to remember to Create HelloWorld Module in Magento 2

You need to keep in mind following high level steps for <b>Creating HelloWorld Module</b>:

  • Step 1: Decide namespace/vendor and create HelloWorld folder inside it ex: app/code/Magehelpers/HelloWorld
  • Step 2: Create module.xml file inside ex: app/code/Magehelpers/HelloWorld/etc/module.xml
  • Step 3: Create registration.php file inside ex: app/code/Magehelpers/HelloWorld/etc/registration.php
  • Step 4: Enable the module ex: php bin/magento module:enable Magehelpers_HelloWorld

Step 1:Decide namespace/vendor and create HelloWorld folder inside it ex: app/code/Magehelpers/HelloWorld

The namespace of module is defined as “VenderName_ModuleName”, where as first part denotes name of vendor like in our case it should be “Magehelpers” and last part denotes name of module like in our case it should be “HelloWorld”. For Example in our case it would be Magehelpers_HelloWorld.

Follow the blow guide to create folder structure and files
app/code/Magehelpers/HelloWorld

Step 2: Create module.xml file inside ex: app/code/Magehelpers/HelloWorld/etc/module.xml

Create etc folder and add module.xml file inside it.
app/code/Magehelpers/HelloWorld/registration.php
Add the following content inside it.

Step 3: Create registration.php file inside ex: app/code/Magehelpers/HelloWorld/etc/registration.php

In this step we will register our module to make it works, create registration.php file
app/code/Magehelpers/HelloWorld/etc/registration.php
Add the following content inside it.

Step 4: Enable the module

We are all done now and successfully create HelloWorld module now we just need to enable it.

Run the below command from root for magento directory to check module status
php bin/magento module:status
You will see module in List of disable Modules: Magehelpers_HelloWorld, follow the below guide to enable it
Run the below command from root of you magento installation directory:

Or alternatively we can also enable our module by adding ‘Magehelpers_HelloWorld’ => 1,in app/etc/config.php file like below.

Now we have create our module and it should be available in module enable list.

After this refresh your Magento 2 website, you will get see following error message in your browser.

Please upgrade your database: Run “bin/magento setup:upgrade” from the Magento root directory.

Lets run following command from root of your magento installation:

After this  refresh your website and if you see your website broken,

then just run below command:

Well done, we have successfully created our first HelloWorld Module, now you can also check your module from magento backend System Configuration -> Advanced -> Disable Modules Output.

Lets Create Controller and Rout to Test your Module

for the controller to work we first need to create route for our module, lets check below how we can do that.

Create Frontend Rout for HelloWorld Module

Note: Routes in Magento is divided into 3 essential parts. 1) Router First Name, 2) Controller and 3) Action, see example below.

http://magehelpers.com/front-name/controller/action

To add route, create route.xml file inside app/code/Magehelpers/HelloWorld/etc/frontend/routes.xml

And place below content inside it

After defining route your module url would be like : www.magehelpers.com/helloworld/

Lets create Controller folder and Action

Create action file inside : app/code/Magehelpers/HelloWorld/Controller/Index/Index.php

And add following content inside it.

After this, you need to clear cache, run following command from root of your magento installation.

php bin/magento cache:clean;

Now module url should like like www.magehelpers.com/helloworld/index/index,

Open above url from your browser, this output “Congrats,you have successfully created Hello World Module” will be display in your browser.

 

Leave a Reply

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

fourteen + 5 =