0

I am trying to develop simple Magento 2 module. Accoding to Composer integration guide I had to put all my dependencies into composer.json file into my module root. According to Default File Structure Guide I had to put my code in app/code directory (on development stage, before release). So the question is how to enable magento to load me dependencies from app/code/vendor-name/package-name/composer.json file?

  1. If I run

bin/magento setup:upgrade

my module is enabled, but dependencies are not loaded to /vendor

  1. If I run

composer update

from magento root dir, my module dependencies are still not loaded

  1. If I run

composer install

from app/code/[my module dir], dependencies are loaded into app/code/[my module dir]/vendor, but not into [magento root]/vendor dir

What is the best practice for module development with composer dependencies?

2 Answers 2

0

This is the basic mistake we do. composer update will include the module to composer.json and composer.lock ONLY IF the module is installed by composer.

In your case you have developed a new module locally which has a composer.json. As your module is not installed by composer it has been manually created in app/code/ location composer has no track of your newly developed module.

So if your module have a third party dependency you have to use composer required command in the Magento root to update the composer.json and composer.lock

composer require xyz/abc

Similar post Magento2: How to include third pary module through composer.json in custom module

0

First add the dependecy with the defined structure, for example for the dependency Liquid\Template, note the php -v that you have, and if is compatible.

In my case, 7.1 so https://github.com/kalimatas/php-liquid/releases/tag/1.2.1

Now set the path acording the composer file, in this case will be: vendor/liquid/liquid and all the files go there. Now edit this 3 files in vendor/composer and add the code.

installed.json

, { "name": "liquid/liquid", "version": "v1.2.1", "version_normalized": "1.2.1", "source": { "type": "git", "url": "https://github.com/Lusitanian/PHPoAuthLib.git", "reference": "fc11a53db4b66da555a6a11fce294f574a8374f9" }, "dist": { "type": "zip", "url": "https://api.github.com/repos/Lusitanian/PHPoAuthLib/zipball/fc11a53db4b66da555a6a11fce294f574a8374f9", "reference": "fc11a53db4b66da555a6a11fce294f574a8374f9", "shasum": "" }, "require": { "php": ">=5.3.0" }, "require-dev": { "phpunit/phpunit": "3.7.", "predis/predis": "0.8.@dev", "squizlabs/php_codesniffer": "2.*", "symfony/http-foundation": "~2.1" }, "suggest": { "ext-openssl": "Allows for usage of secure connections with the stream-based HTTP client.", "predis/predis": "Allows using the Redis storage backend.", "symfony/http-foundation": "Allows using the Symfony Session storage backend." }, "time": "2018-02-14T22:37:14+00:00", "type": "library", "extra": { "branch-alias": { "dev-master": "0.1-dev" } }, "installation-source": "dist", "autoload": { "psr-0": { "OAuth": "src", "OAuth\Unit": "tests" } }, "notification-url": "https://packagist.org/downloads/", "license": [ "MIT" ], "authors": [ { "name": "David Desberg", "email": "[email protected]" }, { "name": "Elliot Chance", "email": "[email protected]" }, { "name": "Pieter Hordijk", "email": "[email protected]" } ], "description": "PHP 5.3+ oAuth 1/2 Library", "keywords": [ "Authentication", "authorization", "oauth", "security" ] }

autoload_namespaces.php

    'Liquid\\Unit' => array($vendorDir . '/liquid/liquid/tests'),
'Liquid' => array($vendorDir . '/liquid/liquid/src'),

autoload_static.php

,
        'Liquid\\Unit' => 
        array (
            0 => __DIR__ . '/..' . '/liquid/liquid/tests',
        ),
        'Liquid' => 
        array (
            0 => __DIR__ . '/..' . '/liquid/liquid/src',
        ),

note in this last file to put the dependency in the right letter. and it is done!

Your Answer

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

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