Path Magento2中的绝对路径

Path Magento2中的绝对路径,path,media,absolute,magento2,Path,Media,Absolute,Magento2,如何获取Magento2中媒体文件的绝对路径? 我编写了一个函数来获取绝对路径,但它不起作用 public function getAbsolutePath() { $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); /** @var \Magento\Framework\Filesystem $filesystem */ $filesystem = $

如何获取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;
    }
试试看,它对我有用

$rootDirectory = Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Framework\Filesystem')
                    ->getDirectoryRead(DirectoryList::MEDIA);
试试看,它对我有用。

app/autoloader.php

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

/**
 * Shortcut constant for the root directory
 */
define('BP', dirname(__DIR__));
人们可以这样做:

$mediaPath = BP.'/pub/media/';
也就是说,通过依赖项注入的Magento\Framework\Filesystem是我首选的方法

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');
使用依赖项注入:

使用类`\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');
用于媒体文件夹

$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();
$directoryList = $objectManager->get('Magento\Framework\App\Filesystem\DirectoryList'); 
$media = $directoryList->getPath('media');
使用依赖项注入:

使用类`\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');
用于媒体文件夹

$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);
您可以使用以下选项:

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

试试看,它对我来说很好….

您可以使用它在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; ?>" />
$objectManager=\Magento\Framework\App\objectManager::getInstance();
$b_url=$objectManager->get('Magento\Store\Model\StoreManagerInterface'))
->getStore()
->getBaseUrl(\Magento\Framework\UrlInterface::URL\u TYPE\u MEDIA);
//此处product_badge是自定义图像属性代码,请将其替换为您的属性。
$\u product=$block->getProduct();
$product\U badge=$\U product->getResource()->getAttribute('product\U badge')->getFrontend()->getValue($\U product);
?>
" />

使用blow code获取您在产品页面上载的图像的绝对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; ?>" />
$objectManager=\Magento\Framework\App\objectManager::getInstance();
$b_url=$objectManager->get('Magento\Store\Model\StoreManagerInterface'))
->getStore()
->getBaseUrl(\Magento\Framework\UrlInterface::URL\u TYPE\u MEDIA);
//此处product_badge是自定义图像属性代码,请将其替换为您的属性。
$\u product=$block->getProduct();
$product\U badge=$\U product->getResource()->getAttribute('product\U badge')->getFrontend()->getValue($\U product);
?>
" />

依靠BP不是一个好办法,BP只能在Magento的底层使用。 Magnetor具有init config值,其中媒体目录可能位于基本路径之外。您应该改为依赖\Magento\Framework\App\Filesystem\DirectoryList对象

在引导中,您可以看到: \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);
    }

}
这意味着如果在引导过程中设置$\u服务器变量:

$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


另外,我还没有测试代码,我只是从内存中查看它,所以如果它出现故障,请告诉我。

依靠BP不是一个好办法,BP应该只用于Magento的底层。 Magnetor具有init config值,其中媒体目录可能位于基本路径之外。您应该改为依赖\Magento\Framework\App\Filesystem\DirectoryList对象

在引导中,您可以看到: \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);
    }

}
这意味着如果在引导过程中设置$\u服务器变量:

$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


另外,我还没有测试代码,我只是从记忆中看到它,所以如果它坏了,请告诉我。

你能解释一下你的答案吗?你能解释一下你的答案吗?