Magento2中的绝对路径

时间:2016-01-19 08:50:33

标签: path media absolute 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;
    }

8 个答案:

答案 0 :(得分:7)

应用程序/ autoloader.php

包含:

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

可以做类似的事情:

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

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

答案 1 :(得分:3)

您可以使用mapto 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 :(得分: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');

尝试它为我工作..

答案 3 :(得分:0)

首先,您需要注入DirectoryList类:

public function __construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Framework\App\Filesystem\DirectoryList $directoryList, array $data = []) {
        parent::__construct($context, $data);
        $this->directory_list = $directoryList;  
    }

之后,您将使用DirectoryList方法检索各种路径,如:

对于根文件夹

$this->directoryList->getRoot();

对于媒体文件夹

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

其他文件夹

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

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

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

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

答案 4 :(得分:0)

你可以用这个:

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

答案 5 :(得分: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; ?>" />

答案 6 :(得分:0)

依靠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确保安装中允许该路径,您需要自己进行验证

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

P.S我还没有测试过代码,我只是从内存中查看了一下,所以如果它破了,请告诉我。

答案 7 :(得分:-3)

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