0 votes

How to use require_once in magento 2

I am trying to include a PHP class in Magento 2.

I use a require_once and create a lib folder in the root of my module, the folder containing the class 'lib / Meli / Meli.php'.

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

All to no avail, any suggestions.

This is my controller 'Controller / Adminhtml / Action / publish.php'.

<?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
  ) {
        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 \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';
    //require_once($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;

    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();
  }
}

?>

my echo $modulePath, returns

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

but it doesn't work for require_once

0voto

May McFly Points 1

I will tell you how I solved this.

In my class, Meli.php add:

namespace Uno\MercadoLibre\Controller\Adminhtml\Action;

In my controller, I added:

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]);

With that I was able to consult my PHP class in magento 2.

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X