Magento 2 Extension Development For Beginner

To create Hello World module, you need to complete the following steps:

  • Step 1: Create the folder of Hello World module
  • Step 2: Create etc/module.xml file
  • Step 3: Create etc/registration.php file
  • Step 4: Enable the module

Step 1: Create the folder of Hello World module

Name of the module is defined as “VendorName_ModuleName”. First part is name of the vendor and last part is name of the module: For example: Expert_HelloWorld. Focus on following guide to create the folders: app/code/Expert/HelloWorld

Step 2: Create etc/module.xml file.

Then, it is necessary to create etc folder and add the module.xml file

app/code/Expert/HelloWorld/etc/module.xml

Contents would be:

Step 3: Create etc/registration.php file

In this step, we will add registration.php as following guide:

app/code/Expert/HelloWorld/registration.php

Contents would be:

Step 4: Enable the module

Finish the step 3, we have already created the HelloWorld module. And we will enable this module in this step

After create the module if you run the command as:

php bin/magento module:status

You should see the module is disable now:

List of disabled modules: Expert_HelloWorld

Follow exact guide to enable the module right now, let run the command as:

php bin/magento module:enable Expert_HelloWorld

Or other way, you can access the file:

app/etc/config.php

You will see a long list of modules there, just add your module as well

‘Expert_HelloWorld’ => 1,

Your module should be available now.

After this step, when you open your website in browser you will get an error saying Please upgrade your database: Run “bin/magento setup:upgrade” from the Magento root directory.

Let run the command:

php bin/magento setup:upgrade

After complete,when you open your website in browser you will see the layout of the website is broken. Please run

php bin/magento setup:static-content:deploy to fix this.

After deploy completed,you can also see your module from backend at System Configuration -> Advanced -> Disable Modules Output.

Now, we will create a controller to test module. Before create a controller, we will create a route for HelloWorld module. Route’s in magento are divided into 3 parts: Route frontname, controller and action as following example:

http://yourhost.com/index.php/frontname/controller/action

To add route, it is necessary to create routes.xml file

app/code/Expert/HelloWorld/etc/frontend/routes.xml

since this is a frontend route, we added it in frontend/ folder else we need to add it to adminhtml/ folder

Content would be:

After define the first part of the route, the URL will be displayed as:

http://<yourhost.com>/helloworld/*

Then, We will continue the controller and action

The folder and file you need to create is:

app/code/Expert/HelloWorld/Controller/Index/Test.php

Contents would be:

After completed, please run php bin/magento cache:clean to check result.

Your URL now should be as:

http://<yourhost.com>/helloworld/index/test

After finish all steps, the output Hello World should be displayed in your browser when you open the URL. We hope our guide is very useful and effective for you.

(Visited 48 times, 1 visits today)