I'm trying to include a PHP class in magento 2.

I use a require_once and create a folder lib in the root of my module and there I put the folder that contains the class lib/Meli/Meli.php

Also try in the project folder lib/lib_web/Meli/Meli.php

All without success, any suggestion

This is my controller Controller/Adminhtml/Action/publicar.php

namespace Uno\MercadoLibre\Controller\Adminhtml\Action;

use \Magento\Backend\App\Action;

class publicar extends Action {
  protected $_session;

  protected $_filesystem;
  protected $_directoryList;

   * @param Action\Context $context
  public function __construct(
        Action\Context $context,
        \Magento\Framework\Filesystem\DirectoryList $directoryList,
        \Magento\Framework\Filesystem $filesystem,
        \Magento\Customer\Model\Session $session
  ) {
        $this->_directoryList = $directoryList;
        $this->_filesystem = $filesystem;
        $this->_session = $session;

   * {@inheritdoc}
  protected function _isAllowed() {
      return $this->_authorization->isAllowed('Uno_MercadoLibre::action_publicar');

   * Publicar action
   * @return \Magento\Framework\Controller\ResultInterface
  public function execute() {
    $appId = '123';
    $secretKey = 'abcdefghijkl';
    $redirectURI = 'https://example.mx';
    $siteId = 'MLM';

    //$path = $this->_directoryList->getPath('lib_web');
    //echo "PATH " . $path.'/Meli/Meli.php';

    //$libPath = $this->_filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::LIB)->getAbsolutePath();

    $mediapath = $this->_filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::APP)->getAbsolutePath();
    $modulePath = $mediapath.'code/Uno/MecadoLibre/lib/Meli/Meli.php';

    echo $modulePath;


    $meli = new Meli($appId, $secretKey);
    $params = array();

    $url = '/sites/' . $siteId;

    $result = $meli->get($url, $params);

    echo '<pre>';
    echo '</pre>';


my echo $modulePath, returns


but it does not help me for the require_once


Please use the following code to the path

use Magento\Framework\App\Filesystem\DirectoryList;

     * @var DirectoryList
    private $directoryList;

     * Constructor
     * @param DirectoryList $directoryList
    public function __construct(
        DirectoryList $directoryList
        $this->directoryList = $directoryList;

$modulePath= $this->directoryList->getPath(DirectoryList::APP).'code/Uno/MecadoLibre/lib/Meli/Meli.php';

Hope this will help you

| improve this answer | |
  • All right with the path, I have an error with the instance of the class PHP Fatal error: Uncaught Error: Class 'Uno\\MercadoLibre\\Controlle\\Adminhtml\\Action\\Meli' not found It does not take into account my path lib/Meli/Meli.php – May McFly Jan 14 '19 at 17:31
  • As per the error "Uncaught Error: Class 'Uno\\MercadoLibre\\Controlle\\Adminhtml\\Action\\Meli" Your class name should be "Meli" and in your code, you have written "publicar ". I think this might be an issue. – Shankar Jan 15 '19 at 3:07

Thank you, for your information, I told you how I resolved this.

In my class Meli.php add:

namespace Uno\MercadoLibre\Controller\Adminhtml\Action; 

In my controller, add:

use \Uno\MercadoLibre\Lib\Meli\Meli; 

Object Manager

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();  
$ml_session = $ objectManager->create('\Uno\MercadoLibre\Lib\Meli\Meli', ['client_id' => $ appId, 'client_secret' => $ secretKey, 'access_token' => $ accessToken]); 
| 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.