Magento2: Module File Structure

Magento 2 Module File structure

In Magento2, there are lots of differences you will find out, due to security purposes. Magento2 looks for the files that make up a module, including configuration files, in particular places inside the module file structure. Follow the predefined file structure to ensure that your module works as expected.

A module can live or can be place now anywhere in Magento root directory. Regardless of where you add it, you must be registered, for Registering your module we need to implement registeration.php file.

A typical file structure for a Magento 2 module:

module-file-structure

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Main Directories

The main directories are:

  • Block — it contains PHP classes as part of MVC vertical implementation of module logic.
  • Controller — it contains PHP classes as part of MVC vertical implementation of module logic.
  • Helper — it contains PHP classes as part of MVC vertical implementation of module logic.
  • Model — it contains PHP classes as part of MVC vertical implementation of module logic.
  • Setup — it contains classes for module database structure and data setup which are invoked when installing or upgrading.

Additional directories

Additionally, there are folders for configuration and other ancillary functions for items like plug-ins, internationalization, and front-end layout files.

  • Api— it contains any PHP classes exposed to the API.
  • etc — it contains configuration files.
  • i18n — it contains localization different files, like language files.
  • Plugin — it contains any needed plugin files.
  • view — it contains non-object-oriented and non-static view level parts of the module such as design templates, email templates, and layout files.

Magento2: Registration of module

For Registering our custom module, we need to create a php file named registration.php in the root of our custom folder and write the below given code

\Magento\Framework\Component\ComponentRegistrar::register(
     \Magento\Framework\Component\ComponentRegistrar::MODULE,
     '_',
     __DIR__
 );

if examine this code then you will see that after the name of module there is a __DIR__ variable which is a php variable to identify the current working directory, it means that you can register your module from any where in Root of magento directory.

Enjoy this Article, don’t miss to comment us or write us if you need to know more about magento2