how to include library in magento 2 using helper as below is the class for magento 1 that is Mage::getBaseDir ,


  namespace vendor\Module\Model;

   require_once(Mage::getBaseDir('lib') '/vendor/libname/class.php');
  • please check my answer. – Rohan Hapani Nov 12 '18 at 11:00

Using Factory Method :

protected $_dir;

public function __construct(
    \Magento\Framework\Filesystem\DirectoryList $dir,
) {
    $this->_dir = $dir;

Now, get path like this below way :

$this->_dir->getPath('lib'); // Output: /var/www/html/yourmagento/lib

You need to use this below line :

|improve this answer|||||
  • thanks for your answer dear @Rohan Hapani, i know this method already but my question was to solve it using require_once – sheraz khan Nov 12 '18 at 11:04
  • 1
    For good practise and as per magento standard way. You should use this answer. – Rohan Hapani Nov 12 '18 at 11:08
  • yes you are right but i am converting task of another developer from m1 to m2, so i have to do in that way – sheraz khan Nov 12 '18 at 11:09

The answer is also available in magento 2 Vendor files and it worked for me


    namespace Vendor\Module\Model;
    require_once __DIR__ . '/../../../../../lib/vendor/LibName/LibClass.php';
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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