skip to Main Content

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’

<?php 
namespace UnoMercadoLibreControllerAdminhtmlAction;

use MagentoBackendAppAction;

class publicar extends Action {
  protected $_session;

  protected $_filesystem;
  protected $_directoryList;

  /**
   * @param ActionContext $context
   */
  public function __construct(
        ActionContext $context,
        MagentoFrameworkFilesystemDirectoryList $directoryList,
        MagentoFrameworkFilesystem $filesystem,
        MagentoCustomerModelSession $session
  ) {
        parent::__construct($context);
        $this->_directoryList = $directoryList;
        $this->_filesystem = $filesystem;
        $this->_session = $session;
  }

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

  /**
   * Publicar action
   *
   * @return MagentoFrameworkControllerResultInterface
   */
  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';
    //require_once($path.'/Meli/Meli.php');

    //$libPath = $this->_filesystem->getDirectoryRead(MagentoFrameworkAppFilesystemDirectoryList::LIB)->getAbsolutePath();

    $mediapath = $this->_filesystem->getDirectoryRead(MagentoFrameworkAppFilesystemDirectoryList::APP)->getAbsolutePath();
    $modulePath = $mediapath.'code/Uno/MecadoLibre/lib/Meli/Meli.php';

    echo $modulePath;

    require_once($modulePath);

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

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

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

    echo '<pre>';
    print_r($result);
    echo '</pre>';
    die();
  }
}

?>

the path of the class returns to me, but it does not help me for the require_once

/var/inetpub/example.mx/app/code/Uno/MecadoLibre/lib/Meli/Meli.php

2

Answers


  1. Chosen as BEST ANSWER

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

    In my class Meli.php add: namespace UnoMercadoLibreControllerAdminhtmlAction;

    In my controller add: use UnoMercadoLibreLibMeliMeli;

    Object Manager

    $objectManager = MagentoFrameworkAppObjectManager::getInstance(); $ml_session = $ objectManager->create('UnoMercadoLibreLibMeliMeli', ['client_id' => $ appId, 'client_secret' => $ secretKey, 'access_token' => $ accessToken]); 
    

  2. I think the solution of your problem the composer.

    Please check it the accepted answer:

    How to I use Composer to autoload classes from outside the vendor?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search