9

I've seen a few things floating around the impermanent internet (i.e. Twitter) that some recent changes to Magento 2 make it possible to install a module into the vendor directory via composer, and Magento 2 will see your module without any symlink-ing tomfoolery.

If this is true, is there a "minimum composer.json" file floating about for module developers to use with their modules to let Magento 2 users install them into their systems?

1

2 Answers 2

6

Update

It looks like several things changed with some latest changes (October 7th) in the develop branch. It now looks like it's possible to support a module living in the vendor directory.

I created two examples of installing modules. One that copies files to app/code and another that registers the module where it resides in the vendor directory.

Copy Strategy: https://github.com/mttjohnson/magento2-sample-module-minimal-copy Registration Strategy: https://github.com/mttjohnson/magento2-sample-module-minimal-register

For development purposes I think utilizing the registration strategy is more useful because the files that are being run by Magento can be directly modified, tested and then committed back to the module repo.

Original Answer

If you have a composer.json file for your module composer package that contains a type of magento2-module then by default files will get copied over to a specified mapped directory in the app/code directory.

{
    "name": "vendorname/module-name",
    "type": "magento2-module",
    "require": {
        "magento/magento-composer-installer": "*"
    },
    "extra": {
        "map": [
            [
                "module",
                "VendorName/ModuleName"
            ]
        ]
    }
}

In this example composer.json the composer package name vendorname/module-name will result in the files for the composer package getting placed in vendor/vendorname/module-name.

The special type of magento2-module is implemented as a composer-plugin in the magento/magento-composer-installer package. That is why I have listed it in the require section. It is this composer-plugin that does the copying of files over into the appropriate magento app/code directory.

The extra: {map: [["composerDir","MagentoDir"]]} section is referenced by the composer-plugin installer to know what part of your composer package to map to what part of your magento directory structure. In the example provided this would take vendor/vendorname/module-name/module and copy files from there to app/code/VendorName/ModuleName.

2
  • is the map section here right? It was my understanding that this pull request made it so you didn't need to a plugin to use composer with Magento modules -- that Magento would look in vendor on its own. github.com/magento/magento2/pull/1206 Commented Jul 23, 2015 at 20:29
  • Reading through some of the comments at the bottom of the PR you mentioned, it looks like though the PR was merged it does not fully support modules living in the composer vendor folder.
    – mttjohnson
    Commented Jul 23, 2015 at 22:02
1

It looks like something like this should do the trick.

{
    "name": "pulsestorm/module-name",
    "description": "A description of your extension",
    "authors": [
        {
            "name": "Robert Hoffner",
            "email": "[email protected]"
        }
    ],
    "require": {},
    "autoload": {
        "psr-4": {
            "Package\\Module\\": "src/path/to/Package/Module/Package/Module",
        },        
        "files": [
            "src/path/to/Package/Module/registration.php",
        ]
    }    
}

The key here is the autoloader section. It sets up up a PSR-4 autoloader that points at your class files, and automatically loads your registration.php. When composer drops the files in vendor and re-generated the autoload files, Magento should be able to see your module.

One neat side effect of this -- code no longer needs to be in app/code!

1
  • I'm confused by this. Everything I read about creating my own module has me putting my files in app/code. 1) How does this work and 2) Why is this neat?
    – kevando
    Commented Mar 21, 2016 at 21:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.