Magento2中的绝对路径

Absolute path in Magento2

如何在 Magento2 中获取媒体文件的绝对路径? 我写了一个函数来获取绝对路径,但是它不起作用。

public function getAbsolutePath()
    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        /** @var \Magento\Framework\Filesystem $filesystem */
        $filesystem = $objectManager->get('Magento\Framework\Filesystem');
        /** @var \Magento\Framework\Filesystem\Directory\WriteInterface $mediaDirectory */
        $mediaDirectory = $filesystem->getDirectoryWrite(Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
        $mediaPath = $mediaDirectory->getAbsolutePath();
        return $mediaPath;
    }
/* @var \Magento\Framework\ObjectManagerInterface $om /

$om = \Magento\Framework\App\ObjectManager::getInstance();

$dir = $om->get('Magento\Framework\Filesystem'); 

$mediadir = $dir->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA); 

$path = $mediadir->create('foldername');

试试它对我有用..

$rootDirectory = Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Framework\Filesystem')
                    ->getDirectoryRead(DirectoryList::MEDIA);

app/autoloader.php

包含:

/**
 * Shortcut constant for the root directory
 */
define('BP', dirname(__DIR__));

可以这样做:

$mediaPath = BP.'/pub/media/';

也就是说,Magento\Framework\Filesystem 通过依赖注入是我的首选方法

使用对象管理器:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$directoryList = $objectManager->get('Magento\Framework\App\Filesystem\DirectoryList'); 
$media = $directoryList->getPath('media');

依赖注入:

使用class`\Magento\Framework\App\Filesystem\DirectoryList

protected $_directoryList;

public function __construct(
    ...
    \Magento\Framework\App\Filesystem\DirectoryList $directoryList
    ...
) {
    $this->_directoryList = $directoryList;  
}

对于根文件夹

$this->_directoryList->getRoot();

对于媒体文件夹

$this->_directoryList->getPath('media');

其他文件夹

// Get app folder
$this->_directoryList->getPath('app');

// Get etc Folder
$this->_directoryList->getPath('etc');

// Get public folder
$this->_directoryList->getPath('pub');

// Get var folder
$this->_directoryList->getPath('var');

你可以使用这个:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$fileSystem = $om->get('Magento\Framework\Filesystem');  
$mediaDirectory = $fileSystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);

您可以在 magento 2 中使用获取媒体和基本绝对路径,例如:

$storeManager = $_objectMedia->get('Magento\Store\Model\StoreManagerInterface');
$currentStore = $storeManager->getStore();

$mediaUrl = $currentStore->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);

$_baseurl = $storeManager->getStore()->getBaseUrl();

试试看它对我来说没问题....

使用 blow 代码获取您在产品页面中上传的图片的绝对 url。

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$b_url = $objectManager->get('Magento\Store\Model\StoreManagerInterface')
                    ->getStore()
                    ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);

// here product_badge is custom image attribute code, replace it with yours attribute.

$_product = $block->getProduct();
$product_badge = $_product->getResource()->getAttribute('product_badge')->getFrontend()->getValue($_product);
?>
<img  src="<?php echo $b_url.'/catalog/product'.$product_badge; ?>" />

依靠 BP 不是一个好方法,BP 应该只在 Magento 的低层使用。 Magneto 具有初始配置值,其中媒体目录可能位于基本路径之外。您应该依赖 \Magento\Framework\App\Filesystem\DirectoryList 对象。

在bootstrap中你可以看到: \Magento\Framework\App\Bootstrap::createFilesystemDirectoryList

class Bootstrap
{

    /**
     * Creates instance of filesystem directory list
     *
     * @param string $rootDir
     * @param array $initParams
     * @return DirectoryList
     */
    public static function createFilesystemDirectoryList($rootDir, array $initParams)
    {
        $customDirs = [];
        if (isset($initParams[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS])) {
            $customDirs = $initParams[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS];
        }
        return new DirectoryList($rootDir, $customDirs);
    }

}

这意味着如果您在 bootstrap:

期间设置 $_SERVER 变量
$tmpDir = '/var/tmpfs_mount';
$applicationDir = '/var/www/html/magento';
$loggingDir = '/var/log/restricted';

$params = $_SERVER;
$filesystemsDirs = $params[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS];

$filesystemsDirs[DirectoryList::CONFIG]    = [DirectoryList::PATH => $applicationDir . "/var/cache"];
$filesystemsDirs[DirectoryList::MEDIA]     = [DirectoryList::PATH => $tmpDir . "/var/cache"];
$filesystemsDirs[DirectoryList::GENERATED] = [DirectoryList::PATH => $tmpDir . "/generated"];
$filesystemsDirs[DirectoryList::CACHE]     = [DirectoryList::PATH => $tmpDir . "/var/cache"];
$filesystemsDirs[DirectoryList::LOG]       = [DirectoryList::PATH => $loggingDir . "/var/log"];

$params[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS] = $filesystemsDirs;

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params);

要通过对象管理器完成,请将 DirectoryList 对象添加到构造函数中:

use \Magento\Framework\App\Filesystem\DirectoryList;

class MyModuleClass
{
  /**
   * @var DirectoryList
   */
  private $directoryList;

  /**
   * @param DirectoryList $directoryList
   */
  public function __construct(DirectoryList $directoryList)
  {
      $this->directoryList = $directoryList;
  }

  /**
   * @var DirectoryList
   */
  private $mediaPath;

  public function getMediaAbsolutePath(){
       if (!$this->mediaPath){
           $this->mediaPath = $this->directoryList->getPath(DirectoryList::MEDIA);
       }
       return $this->mediaPath;
  } 

}

请注意,这只会为您提供路径,但不会通过 Magento 2.3 中引入的 PathValidator 进行验证。这个 PathValidator 确保路径在安装中是允许的,你需要自己验证它

vendor/magento/framework/Filesystem/Directory/PathValidator.php

P.S我还没有测试过代码,我只是凭记忆看了一下,所以如果它坏了请告诉我。