Magento – Magento 2 Custom Module file Structure

magento-2.0magento2

I developed a custom magento module and was trying to test it using the validate_m2_package tool as part of marketplace upload. I am little confused on folder structure for Magento module.

I developed the modules using the following
1. app/code/vendorname/modulename/etc/module.xml
app/code/vendorname/modulename/composer.json
app/code/vendorname/modulename/registration.php

It was working fine functional as required for my purpose. But when I am package it to zip file with vendorname folder as root and run it against the validation tool it throws error saying "composer.json" is unexpected place. But when i package module file as root and validate it passes through all validation. is the folder structure correct? if yes, is the validation script is having a bug. if no, am i suppose to package only module file for marketplace upload?

Best Answer

The folder structure is correct for development. However, for a Marketplace upload, you should only package the module directory. Magento 2 extensions will be installed using the Component Manager, which uses Composer under the hood. The Component Manager and Composer will take care of extracting and installing your package to the correct location. Generally, this will be under the vendor/ directory. Based on the information in the registration.php and composer.json's "autoload" section, the module will be installed in the following structure:

magento2/
|+- app/
|+- bin/
|+- vendor/
    |+- <Vendor name>
        |+- <Module name>
            |+- composer.json
            |+- registration.php
            |+- ...
|+- setup/
|+- ...

If your registration.php and composer.json files are set up correctly, then the Magento application will be able to identify and use that module. Furthermore, Composer will take care of any namespaces you have created and any namespaces you use in your module.

The aim of this tool is to validate your package structure in preparation for an upload to the Marketplace.

Related Topic