MAGENTO

Create custom module in Magento 2.1

In this tutorial I am going to present how to create custom module in Magento 2.1. Below is a video representation to get you understand how exactly the commands work and how to create folder structure for a basic module in Magento 2.1 version. Creating a custom module in Magento 2.1 is more simpler than Magento 1.x versions.

Folder structure to create custom module in Magento 2.1

Understanding the folder structure for any framework is an important and complex part. Once you are acquainted with the folder structure in Magento 2.1 then there is no big deal in calling classes in any module/controller.

  1. PHP 7.x
  2. Windows XAMPP
  3. Composer 1.4v
  4. NetBeans v8

I assume that you have successfully installed Magento 2.1 version with all its required php extensions. Now open terminal and route out to your project root directory and run the following command. This step is necessary to make work all js, css and links properly. Here my project name is demo.

The folder structure

Create custom module in Magento 2.1
Create custom module in Magento 2.1

In this demo I am creating a custom module “Custom” under package Excellence

Create files and folder for custom module

When you fresh install Magento 2.1 version you will find no “Code” folder inside app folder. For this we manually have to create a folder “Code” inside app folder.

Inside code folder only module package folders are present. Here my package name is Excellence

app/code/Excellence

Inside package folder all your module folders are present. In this demo my module name is Custom.

app/code/Excellence/Custom

Unlike Magento 1.x version we do not need to head out of our module folder to write any xml. We are just going to write all our xml, php, controller etc inside module folder only.

Create etc module.xml

The main folder inside which we are going to  write our module.xml file is etc folder. Unlike Magento 1.x version this is the same xml as config.xml file but a short version of it.

In Magento 2.1 we need to write a small piece of code to create a module.

app/code/Excellence/Custom/etc/module.xml

Create frontend routes.xml

In this folder we are going to write frontname or route names for our module that is going to be reflected in url.

app/code/Excellence/Custom/etc/frontend/routes.xml

Make sure that the route id and frontName are same otherwise it will create issue later.

Register the Magento module

To register a Magento 2.1 module we need to write a registration.php file. In following path

app/code/Excellence/Custom/registration.php

The register function of  \Magento\Framework\Component\ComponentRegistrar class takes 3 parameters that is “Type”, “Name” and “Path”. This file is executed by CLI to register a module.

Check module status

To check a module status we need to run following command

It will list out all enabled and disabled modules. Our module is inside disabled module section. We can enable a module in two ways.

  1. Via CLI
  2. Making ‘Excellence_Custom’ => 1 in  app\etc\config.php

Enable module Via CLI

To enable a module via CLI run the following command in website’s root directory

Congratulations, our magento custom module is ready to be used. To test our module lets create a controller and echo something in it.

Create a controller

Controller in magento 2.1 version follows its basic zend architecture. A controller in Magento plays a very important role. To create a Magento controller we need to create a folder “Controller” inside our module then create another folder same as our module name inside Controller folder then create a php  action file inside it.

app/code/Excellence/Custom/Controller/Custom/Index.php

The execute() method is same as indexAction method of Magento 1.x v. Our controller is now ready for “Custom” module. Lets invoke the controller as final step of this tutorial.

Invoke the controller

Make sure you are well aware of the following paths

  1. app/code/Excellence (Path to our package)
  2. app/code/Excellence/Custom(path to module)
  3. http://mydomain.com/excellence/custom/index(url to invoke custom controller action)

Now hit http://localhost:81/demo/index.php/excellence/custom/index

Create custom module in Magento 2.1
Create custom module in Magento 2.1

Conclusion

In this tutorial we have successfully created a custom module in Magento 2.1 version. Magento 2.1 version is a great choice for you e-commerce website. Its far better and faster that Magento 1.x version. Thank you for reading and watching.

Share

45 Comments

  1. This design is spectacular! You definitely know how to keep a reader amused.

    Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Wonderful
    job. I really loved what you had to say, and more
    than that, how you presented it. Too cool!

    Reply

Leave a Comment

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