Skip to main content
edited tags
Link
sv3n
  • 11.7k
  • 7
  • 41
  • 73
Source Link

Magento 2.2 : How To Specify External Library/Dependency In Your Custom Magento Module Deployed under the app/code folder

I have a (Not Open-Source) custom Magento 2.2x module that I'm working on, that has a dependency on an external composer package (bugsnag/bugsnag)

Here's how my project is currently structured:

Current Project Structure

Project Structure

File - module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Gsd_Test" setup_version="0.1.0">
    </module>
</config>

File - composer.json

{
    "name": "gsd/test",
    "description": "",
    "require": {
        "php": "7.*",
        "bugsnag/bugsnag":"^3.0"
    },
    "suggest": {

    },
    "type": "magento2-module",
    "version": "0.1.0",
    "license": [

    ],
    "autoload": {
        "files": [
            "registration.php"
        ],
        "psr-4": {
            "Gsd\\Test\\": "",
            "Bugsnag\\" : "vendor/bugsnag/bugsnag/src/"
        }
    },
    "extra": {
        "map": [
            [
                "*",
                "Gsd/Test"
            ]
        ]
    }
}

Controller - Gsd\Test\Controller\Adminhtml\Index\Index.php

<?php

namespace Gsd\Test\Controller\Adminhtml\Index;

class Index extends \Magento\Backend\App\Action
{
    protected $_publicActions = ['index'];

    public function execute()
    {
        echo "Hello World";
        $bugsnag = \Bugsnag\Client::make('XXXXXX');
        \Bugsnag\Handler::register($bugsnag);

        $bugsnag->notifyException("Testing ");
    }

}

Registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Gsd_Test',
    __DIR__
);

BUT, attempting to access that Controller (http://m22.localhost.net/admin/gsdtest/index/index) results in the following error:

Application Error

Looks like, for whatever reason, the Autoloader ignores the content within the app/code/Gsd/Test/vendor/bugsnag/bugsnag/src folder at run-time.

(Ironically, PhpStorm doesn't report any error during development per se)

Now, if I add "bugsnag/bugsnag" as a composer dependency at the MAGENTO_HOME folder level, i.e, if I run the following commands:

cd $MAGENTO_HOME
composer require bugsnag/bugsnag 3.*
composer update
composer dump-autoload

then my controller works just fine.

So, my questions are:

  • I recall this used to work on Magento 2.1x (i.e. having the composer dependency inside the module specific composer.json) about 2 - 3 months back. But, it no longer works on Magento 2.1x or Magento 2.2x ... What changed??

  • Is there a way to specify module specific composer.json dependencies? If not - how can one "ship" a custom-module - that has dependencies on 3rd party libraries - so that it can be deployed directly into the app/code folder? ( Requiring customers deploy the module using composer may not work for all businesses/situations )