Magento2中的绝对路径

问题描述 投票:0回答:8

如何在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;
    }
path media absolute magento2
8个回答
6
投票

app / autoloader.php

包含:

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

一个人可以做类似的事情:

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

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


3
投票

您可以用来获取媒体并在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();

尝试一下对我来说很好。...


2
投票
/* @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');

尝试它对我有用。.


1
投票

依靠BP并不是实现这一目标的好方法,BP只能在Magento的较低层中使用。Magneto具有初始化配置值,其中媒体目录可能位于基本路径之外。您应该改为依赖\ 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);
    }

}

这意味着如果您在引导过程中设置$ _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可确保安装中允许该路径,您需要自己进行验证]

供应商/ magento /框架/文件系统/目录/PathValidator.php

P.S,我还没有测试代码,我只是从内存中查看了一下,所以如果发现问题,请通知我。


0
投票

您可以使用此:

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

0
投票

使用打击码获取您在产品页面中上传的图像的绝对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; ?>" />

0
投票

使用对象管理器:

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

其他文件夹

// 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');

-3
投票
$rootDirectory = Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Framework\Filesystem')
                    ->getDirectoryRead(DirectoryList::MEDIA);
© www.soinside.com 2019 - 2024. All rights reserved.