Create a Magento2 Module

Create a Magento2 module to hold our code

Posted on March 16, 2016 in Magento2

Create a Magento2 Module

  • Create module.xml file at app/code/VendorName/ModuleName/etc/module.xml;
    
    <?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="VendorName_ModuleName" setup_version="0.0.1" />
    </config>
    
    
  • Next we will add our module to the global module list at app/etc/config.php.
    
    #File: app/etc/config.php
    <?php
    return array (
      'modules' => 
      array (
        'Magento_Store' => 1,
        'Magento_AdvancedPricingImportExport' => 1,
        'Magento_Directory' => 1,
        //...
        'VendorName_ModuleName'=> 1
      ),
    );   
    
    
  • There’s one more step you’ll need to take. In addition to the files mentioned above, every module needs a registration.php file
    
    #File: app/code/VendorName/ModuleName/registration.php
    <?php
    \Magento\Framework\Component\ComponentRegistrar::register(
        \Magento\Framework\Component\ComponentRegistrar::MODULE,
        'VendorName_ModuleName',
        __DIR__
    );
    
    

These files are identical for all modules, with the exception of the VendorName_ModuleName. There are a few different ways to check if a module’s installed correctly. One is to use the CLI application’s :

php bin/magento module:status



After installing a new module into the system, you may see the following error: “Please upgrade your database”, then you just run

php bin/magento setup:upgrade

If you saw your VendorName_ModuleName in the list, it means you successfully register your new module.


comments powered by Disqus