Magento 2 Module File structure

Main components

The main component folders are:

  • Block — contains PHP classes as part of MVC vertical implementation of module logic.
  • Controller — contains PHP classes as part of MVC vertical implementation of module logic.
  • Helper — contains PHP classes as part of MVC vertical implementation of module logic.
  • Model — contains PHP classes as part of MVC vertical implementation of module logic.
  • Setup — 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— contains any PHP classes exposed to the API.
  • etc — contains configuration files.
  • i18n — contains localization files.
  • Plugin — contains any needed plugin files.
  • view — contains non-object-oriented and non-static view level parts of the module such as design templates, email templates, and layout files.

Add your own

You can also add any other folders you need for your module.

Build the file structure

Add a folder and give it the name of your module. Use a module name that is descriptive, but not too long. For example,

mkdir module-catalog
cd module-catalog

You can add subfolders inside your module folder as you need them (Block, Controller, Model, view, and so on).

Or, you could just add them at once:

mkdir Api Block Controller etc Helper i18n Model view

Make sure you add the etc folder; it is required, and is where most of the configuration files, includingmodule.xml, are kept.

Be aware that the standard placement of the <ModuleName> directory within the overall Magento file structure is app/code/<Vendor>/<ModuleName>/etc/. However, if you are creating a new module for distribution, you can just create the <ModuleName> directory and the required directories within it.

Leave a Reply

Your email address will not be published. Required fields are marked *