Magento 2 – How to Use Composer for Extension

magento-2.1magento2

Im developing an extension and would want to use some composer extensions in it. So I have a composer.json file in app/code/Vendor/Extension/ path.

Do I simply run composer update in the extension directory? This will put the vendor directory into the extension right? Is there a way I can have magento pick up the composer.json of my extension when I run a update from root?

Also most importantly what is the best practice in this?

Best Answer

There are two options here:

  1. Described in the comments: create a package or a repository from your extension. If you don't want to push it to the world, you can create a Git repo locally. The repo should contain just your extension and composer.json in the root. Then link your extension's repository as a package to the Magento project in Magento's composer.json (see an example in https://getcomposer.org/doc/04-schema.md#repositories). Use "dev-xxx" version, where xxx is the branch name in your repo - then Composer will always be fetching the latest commit. Then, when you run composer update your package will be loaded with all its dependencies.

  2. Leave file structure as is and duplicate all dependencies, autoload and any other deployment-specific information from your extension's composer.json you the Magento's root composer.json. Then when running composer update (in Magento root) all dependencies of your extension will be deployed.

Related Topic