0
votes

I want to use a library that's registered with composer in Magento 2 admin. The library is https://packagist.org/packages/flagshipcompany/flagship-api-sdk

I have a custom module that adds a button to admin order view and on the click of the button, the controller is called. I need to use this library in that controller.

I'm very new to composer and Magento. I'm not even sure if my composer.json is correct.

I have executed composer install in my module directory and I have the vendor directory. The directory structure is

Magento2/app/code/MyCompany/MyModule/

Controller/Adminhtml/ControllerName/Index.php

etc/adminhtml/di.xml

etc/adminhtml/routes.xml

etc/module.xml

Plugin/....

composer.json

composer.lock

vendor/[all the composer generated directories]

I need to use require 'vendor/autoload.php' in Controller/Adminhtml/ControllerName/Index.php. But everytime I put this line of code, it crashes. Also, once I am able to use autoload.php, I need to create an object of class Flagship which is present at vendor/flagshipcompany/flagship-api-sdk/Shipping/Flagship.

TIA

1
If you are not sure about your composer.json then paste it here, we will verify it :) - Damian Dziaduch
What happens when you install it? Run 'composer require flagshipcompany/flagship-api-sdk' in the same directory as your compose.json - what happens? - Alex.Barylski
Updated the question with more details. Thanks! - coderGeek

1 Answers

0
votes

Composer is a php dependency manager and it can be used with Magento as well. Here are the steps to install composer and check composer.json:

First open the composer.json file and add the following code to use the "flagship-api-sdk" package:

"require": {
        "php": ">=7.1.0",
        "flagshipcompany/flagship-api-sdk": "",
        "phpunit/phpunit": "^6.5",
        "tightenco/collect": "^5.7"
    }

Then go to the folder where you have installed Magento and using terminal/command prompt, run the following commands:

composer install

Make sure you are connected to the internet, this process will take 3-5 minutes. After this you can check if the dependencies are installed using

composer show

P.s Make sure you have php version 7.1.0 or above and using Magento 2.2