Magento – How to use a library using composer in a Magento 2 module

3rd-party-libraryautoloadercomposerlib

At the moment I'm developing a module for Magento 2. I'm using a third party library ("twizo/lib-api-php") and installed it using composer. When I use a class from this library, Magento gives me the error "Class Twizo\Api\Twizo' not found". How can I autoload the files from this vendor directory? My composer.json file looks like this now:

{
  "name": "twizo/authentication",
  "type": "magento2-module",
  "version": "0.1.0",
  "autoload": {
    "psr-4": {
      "Twizo\\Authentication\\": ""
    },
    "files": [
      "registration.php"
    ]
  },
  "require": {
    "twizo/lib-api-php": "^0.10.0"
  }
}

When I remove the files section of composer.json it works. But according to the Magento package validation tool each module's composer.json file has to contain a field section so that's no really an option.

If I require the autoload.php file manually like this: require __DIR__ . '\..\..\..\vendor\autoload.php';, Magento gives the error that my module is already defined, so I think it will do the autoload twice.
How can I fix this?

Module 'Twizo_Authentication' from 'C:\xampp\htdocs\magento\app\code\Twizo\Authentication' has been already defined in 'C:/xampp/htdocs/magento/app/code/Twizo/Authentication'.

Best Answer

Related Topic