【问题标题】:how to use require_once in magento 2如何在 magento 2 中使用 require_once
【发布时间】:2019-06-06 18:34:30
【问题描述】:

我正在尝试在 magento 2 中包含一个 PHP 类。

我使用 require_once 并在模块的根目录中创建了一个文件夹 lib,然后我将包含类 'lib / Meli / Meli.php' 的文件夹放在那里

也可以在项目文件夹'lib/lib_web/Meli/Meli.php'中试试

都没有成功,有什么建议

这是我的控制器'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();
  }
}

?>

类的路径返回给我,但它对 require_once 没有帮助

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

【问题讨论】:

    标签: php class magento2 external


    【解决方案1】:

    我认为你的问题的解决方案是作曲家。

    请检查接受的答案:

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

    【讨论】:

      【解决方案2】:

      谢谢你的信息,我告诉你我是如何解决这个问题的。

      在我的班级 Meli.php 中添加: namespace Uno\MercadoLibre\Controller\Adminhtml\Action;

      在我的控制器中添加: use \Uno\MercadoLibre\Lib\Meli\Meli;

      对象管理器

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-05-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-17
        • 2011-09-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多