In this post, we will see how modules are created in Magento 2 and how it is different from Magento 1.

Module directory structure comparison
Magento 1

Create your module at:

app/code/local

For module: Ashutosh/Pandey , the directory structure is:

<Ashutosh>
     <Pandey>
          <Block>
          <controllers>
          <etc>
            config.xml
          <Helper>
          <Model>
          <sql>
Magento 2

Create your module at:

app/code

For module: Ashutosh/Pandey , the directory structure is:

<Ashutosh>
     <Pandey>
          <Api>
          <Block>
          <Controller>
          <etc>
             <adminhtml>
                  routes.xml
                  .....
             <frontend>
                  routes.xml
                  .....

             <webapi_rest>
             <webapi_soap>

             config.xml
             module.xml
             .....
          <Helper>
          <i18N>
          <Model>
          <Plugin>
          <view>
Creating a basic module in Magento 2
  1. Make sure code folder exists under app folder, if not, create it.
  2. We need to create following directory structure:
    <Mymodule>
         <Test>
              <etc>
                  module.xml
         registration.php
  3. Create a new folder named Mymodule under code and then Test inside Mymodule folder.
  4. Create a new folder named etc inside Test folder.
  5. Create file module.xml inside etc folder with following content:
    <?xml version="1.0"?>
    
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
        <module name="Mymodule_Test" setup_version="0.0.1"/>
    </config>
  6. Create a new file named registration.php under Test folder. This file will be used to register our module to Magento. Add following code to it:
    <?php \Magento\Framework\Component\ComponentRegistrar::register(\Magento\Framework\Component\ComponentRegistrar::MODULE,'Mymodule_Test',__DIR__);
  7. Start command prompt and navigate to your project folder. Execute the following command to enable our new module:
    php bin/magento module:enable Mymodule_Test

    The above command will add following entry in the app/etc/config.php file:

    'Mymodule_Test' => 1
  8. Next, execute the following command to let Magento read all the modules:
    php bin/magento setup:upgrade

    There will be a new entry added to the setup_module table in the database.

Your module is ready now. To see all enabled modules, we can execute following command:

php bin/magento module:status

This will list all the enabled modules in the current project.

To disable an existing module, execute the command:

php bin/magento module:disable Mymodule_Test