Create Basic Module of Magento 2

Before I dig into coding parts of our first Module development using Magento 2:-
Let me add some input into this direction that will further help us to understand further .

Couple of things about Magento 2

  1. Magento 2 is not based on MVC

Magento 2 is not based on MVC (Model View Controller) pattern. Instead its based on MVVM (Model Veiw View Model) model. Lets understand the different between MVC and MVVM (Model View View Model) .

In Magento 1, Based on the URL request it calls corresponding controller and action name. For example if url is www.abc.com/account/register . Based on the URL name it was automatically invoking account controller and register action under it.

Now this is not the case in Magento 2, Now every controller has one execute method that gets called. Now, instead of multiple action name under one controller , now only one execute method for each controller so if there are 10 actions to be moved in magento 2 then, 10 different controller to be created.

  1. There is no code pool in Magento 2. All code is written into app/code/Module/Code

Lets deep dive into our first module development

The name of this module would be Cutehits_First.Where Cutehits is the Module’s company name and First is the name of the module.

To start into development, just Create a folder under app/code/<namespace>/<modulename>. For example app/code/Cutehits/First

This module development has been distributed into four parts:

Part 1: Creating module configuration
Part 2: Registering module
Part 3: Create module Routing
Part 4: Creating controller

Part 1: Creating module configuration

Now create a folder etc and create a file module.xml under it. See the content of Cutehits\First\etc\module.xml.

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">

    <module name="Cutehits_First" setup_version="1.0.0">

    </module>

</config>

Code Analysis Part 1

Note: The only important part under this code is

<module name="Cutehits_First" setup_version="1.0.0">    </module>

Rest is boilerplate code. There are only 2 parts to understand

  • name -> Name of <namespace>_<modulename>
  • setup_version -> if there is any db setup with your extension to be deployed

Part 2: Registering module

Now create a file under <namespace>\<modulename>\registration.php. see the content of Cutehits\First\registration.php


\Magento\Framework\Component\ComponentRegistrar::register(

    \Magento\Framework\Component\ComponentRegistrar::MODULE,

    'Cutehits_First',

    __DIR__

);

Code Analysis Part 2
The important part of above snippet is <namespace>_<modulename> remaining is boilerplate code.

Part 3: Create module Routing

The next important part is routers. create a file routes.xml. See the content of Cutehits\First\etc\frontend\routes.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">

    <router id="standard">

        <route id="first" frontName="first">

            <module name="Cutehits_First" />

        </route>

    </router>

</config>

Code Analysis Part 3

<route id=”first” frontName=”first”>

<module name=”Cutehits_First” />

</route>

This part is important to understand.

Every route has a unique id and has their unique frontname. In our case first is the front name. The next part is moduel name that shows which module handles the route.

Part 4: Creating controller

The next important part is controller:

Here its notable that in Magento 2 every action has their own controller file.so if its written that <module name=”Cutehits_First” /> . It means no controller name is defined. In this case default controller name is Index controller. Ideal format is <module name=”namespace_modulename_Controllername” />

So lets create folder path like <namespace>\<modulename>\Controller\Index\Index.php

See the code of Cutehits\First\Controller\Index\Index.php


namespace Cutehits\First\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{

 public function execute()
 {

 echo "Hello World...";
 }
}

Code Analysis Part 4
namespace Cutehits\First\Controller\Index; :- We have created a new namespace name that can further be utilized

use Magento\Framework\App\Action\Context; :- consuming the namespace, see trait.

 

Now created a class named Index and created a protected variable $_resultPageFactory so that we can consume it in any child class.

now constructor comes with 2 arguements (see dependency injection).

Next important part is execute method . Every controller action class has a method execute that actually gets called by the magento 2.

So we have just written ‘Hello world ‘ there.

The following two tabs change content below.

Chandra Shekhar

GCP Architect
Chandra Shekhar Pandey is Google certified Cloud engineer, I am Magento2 Trained developer. Having huge experience in designing cloud solution. I have around 12 years of experience with world enterprise IT companies and fortune 500 clients. During my architecture design I am always caring about high availability, fast performance and resilient system. From the programmer background I have huge experience in LAMP stack as well. Throughout my carrier I have worked on Retail, E-Learning, Video Conferencing and social media domain. The motive of creating cutehits was just to share the knowledge/solutions I get to know during my day to day life so that if possible I can help someone for same problems/solutions. CuteHits.com is a really a very effort for sharing knowledge to rest of the world. For any query/suggestion about same you can contact me on below details:- Email: shekharmca2005 at gmail.com Phone: +91-9560201363

Latest posts by Chandra Shekhar (see all)

You may also like...