dpfl37651 2019-01-11 19:58
浏览 102
已采纳

如何在magento 2中使用require_once

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

?>

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条回答 默认 最新

  • du1913 2019-01-21 19:58
    关注

    Thank you, 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]); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮