In this post we will learn how to add external library in magento 2. As a magento 2 extension developer, it is very important to know how to add external library in magento 2 the right way . Payment gateways ,shipping api, social networking etc, they all provide their language specific library, that we need to include in our magento extension to connect with them and obviously we need to add them in the right way as magento does.
When you will see the magento 2 folder structure, you will notice vendor folder in the root, when you will open this folder you will notice many libraries such as braintree, symphony, doctrine etc, they all are provided when you will download magento 2 but what if the extension you are creating require another library for example if you want to create stripe payment method in magento 2 you will need to add stripe library for the api calls .
Magento 2 uses various technologies and composer is one of them ,composer is used to manage dependency in the project the best way to add any external library without going into the code is composer, magento have a composer.json file in its root. For adding stripe library in the vendor folder, you just need to run this command from the command line :
For this command you must install composer on your server ,if you are not familiar with composer go through this link first : https://getcomposer.org/doc/00-intro.md
after installing the composer, go to your Magento 2 root folder and run the below command from the terminal :
composer require stripe/stripe-php
In the above command “stripe/stripe-php” is the package name you can found this on packagist .
after executing the above command the latest version of the stripe library will be installed on your Magento 2 extension and it will be autoloaded in your project. You can find the changes in two places in the Magento first in the composer.json file in the Magento 2 root:
and the second you will find its folder in Magento 2 vendor folder in the root:
That’s all, you will find it very easy to do, but in case you have any doubts please comment below.
Thanks 🙂
2 comments