Magento2 Create Simple Module

Today I am going to demonstrate you how to create a basic custom module in magento2. Prerequisites : This tutorial assumes that you have installed magento with latest version on your machine and you have basic knowledge in magento and magento2. Create Module Directories: To get started you must create module directories inside app/code folder. If Code folder doesn’t exist create it. Module directories involve <namespace>/<module_name> So the directory structure should be: app/code/<namespace>/<module> Create a Simple HelloWorld module: To get started we have to go through the following steps:
  • Create module directories as stated above
  • Create module.xml configuration file to declare module
  • Create Registration.php to register the module
  • Create a router.xml to declare the route
  • Create a simple controller and action method
  • Enable your module
  • Setup & deploy your module
  1. Create Module Directories :In module we will use Know as Vendor name and HelloWorld as our module name. So our directory structure looks like: app/code/Know/HelloWorld
  2. Create module.xml Configuration file to declare module:To create module we need to add the configuration file under etc directory and add module.xml :
    app/code/Know/HelloWorld/etc/module.xml
    Add following content to 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="Know_HelloWorld" setup_version="1.0.0" />
    </config> 
    
    In Above file we are declaring our module with name "Know_HelloWorld" along with version 1.0.0
  3. Create Registration.php to register the module

    ComponentRegister class is used to register our module in magento system. The name of file is kept "Registration.php" and is kept in the root directory of our module, i.e:

    app/code/Know/HelloWorld/Registration.php

    Add the following code in your Registration.php file

     
    <?php 
      \Magento\Framework\Component\ComponentRegistrar::register(
        \Magento\Framework\Component\ComponentRegistrar::MODULE,
          'Know_HelloWorld',
        __DIR__
       );
    ?>
    

    Specify your

    <Vendor>_<MduleName>
    in second parameter of register method of ComponentRegistrar class.

  4. Create a routes.xml to declare the route

    The request Url contains the following format:

    http://examplestore.com/<router>/<controller>/<action>

    We are creating frontend controller. So in order to do that we need to define our router for frontend controller by creating folder frontend under etc directory of module and create a new file with name routes.xml under our newly created directory:

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

    Add the below content in file:

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
        <router id="standard">
            <route id="knowTheMage" frontName="helloworld">
                <module name="Know_HelloWorld" />
            </route>
        </router>
    </config>
    

    After definition, we have our router name helloworld as mentioned in above code under the attribute frontName. The id of module should be unique. We are also specifying our module for that specific router.

    The request would be :

    http://examplestore.com/helloworld/*/*
  5. Create a simple controller and action method

    In order to define the route completely we need to create Controller and an Method inside it.

    To create a controller, create a Controller directory inside your module’s root directory.

    Now let’s assume that we want our url to be http://examplestore.com/helloworld/index/welcome

    So from above url we already have our router i.e helloworld We just need to create index and welcome. Here index is the name of directory we will creating and serves as ControllerName here in magento2 while the other one is our method name which will a class Name and a php file exactly i.e Welcome.php

    app/code/Know/HelloWorld/Controller/Index/Welcome.php

    Add following content your Welcome.php file:

    <?php
       namespace Know\HelloWorld\Controller\Index;
       
       use \Magento\Framework\App\Action\Context as Context;
       class Welcome extends \Magento\Framework\App\Action\Action {
          public function __construct(Context $context) {
             return parent::__construct($context);
          }
    
          public function execute()  {
             echo 'Welcome! Magento2 is awesome.';
          }
       }
    ?>
    
  6. Enable your module

    Magento system knows about the existance of our module now. To know about that we need to run a command via composer or command line tool by going to root directory of magento installation. Run following command:

    php bin/magento module:status

    It will list down the list of disabled and enabled module. Our module will be listed under:

    List of disabled modules:
    Know_HelloWorld
    

    So to enable our module so that magento knows in enabled list we need to run the following command via command line / composer:

    php bin/magento module:enable Know_HelloWorld

    In result:

    The following modules have been enabled:
    - Know_HelloWorld
    
  7. Setup & deploy your module

    Magento needs to check and upgrade module database. Also to properly enable and deploy our module we need to run the following command:

    php bin/magneto setup:upgrade

    To make sure your module exists go to admin > Stores > Configuration > Advanced > Advanced and there you will see your module is listed there.

    Now if you open up http://examplestore.com/helloworld/index/welcome you will the output by the controller:

    Welcome! Magento2 is awesome.
    

You can directly download the module from : https://github.com/arsalanworld/Know_HelloWorld