如何在magento 2中的phtml文件中获取媒体目录路径?


14

为了获得媒体目录路径,使用了以下方法,但是返回错误。

$om = \Magento\Core\Model\ObjectManager::getInstance();

$directoryList = $om->get(\Magento\App\Filesystem\DirectoryList::class);

$pubMediaDir = $directoryList->getPath(\Magento\App\Filesystem\DirectoryList::MEDIA);

请帮助我找到解决方案。


1
我不清楚您的问题。您可以解释更多细节吗?此外,我们可以看一下:magento.stackexchange.com/a/155238/33057
Khoa TruongDinh,2017年

Answers:


23

与其使用direct object manager,不如使用它

use Magento\Framework\App\Filesystem\DirectoryList;

protected $_filesystem;

public function __construct(
    \Magento\Framework\Filesystem $filesystem
)
{
    $this->_filesystem = $filesystem;
}

现在您可以通过以下方式进行媒体传播

$mediapath = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath();

编辑

如果要使用对象管理器,则可以使用它(不推荐)

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

$fileSystem = $objectManager->create('\Magento\Framework\Filesystem');
$mediaPath = $fileSystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)->getAbsolutePath();
echo $mediaPath;
exit;

它会返回这样的错误:“ Uncaught TypeError:传递给namespace \ Module \ Controller \ Index \ Upload :: __ construct()的参数2必须是Magento \ Framework \ Filesystem的实例,未给出任何给定,”
Rita Jose

是的,尝试执行di:compile,然后重试一次:)
Keyur Shah

di:编译完成,但再次返回错误:(“”可恢复错误:无法将类Magento \ Framework \ Filesystem \ Directory \ Read的对象转换为/ opt / lampp / htdocs / magento214 / app / code / namespace中的字符串/Customtab/Controller/Index/Upload.php,第18行“
丽塔·何塞

看到我的编辑,如果您想使用直接对象管理器@RitaJose
Keyur Shah,

哇:D ..非常感谢..编辑过的作品很好:)
丽塔·何塞

7

首先,您需要将DirectoryList类注入到您的Magento 2构造函数中:

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

之后,您将可以访问DirectoryList方法以检索各种路径。例如,要获取媒体文件夹,您可以使用:

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

其他可能的用途是:

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

/* Get configuration folder */
$this->directory_list->getPath('etc');

/* Get libraries or third-party components folder */
$this->directory_list->getPath('lib_internal');

/* Get libraries/components that need to be accessible publicly through web-server folder */
$this->directory_list->getPath('lib_web');

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

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

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

/* Get temporary files folder */
$this->directory_list->getPath('tmp');

/* Get file system caching directory (if file system caching is used) */
$this->directory_list->getPath('cache');

/* Get logs of system messages and errors */
$this->directory_list->getPath('log');

/* Get file system session directory (if file system session storage is used) */
$this->directory_list->getPath('session');

/* Get directory for Setup application*/
$this->directory_list->getPath('setup');

/* Get Dependency injection related file directory */
$this->directory_list->getPath('di');

/* Relative directory key for generated code*/
$this->directory_list->getPath('generation');

/* Temporary directory for uploading files by end-user */
$this->directory_list->getPath('upload');

/* Directory to store composer related files (config, cache etc.) in case if composer runs by Magento Application */
$this->directory_list->getPath('composer_home');

/* A suffix for temporary materialization directory where pre-processed files will be written (if necessary) */
$this->directory_list->getPath('view_preprocessed');

/* Get template minification dir */
$this->directory_list->getPath('html');

它返回媒体的浏览器网址...我需要媒体的文件夹路径
Rita Jose

请查看我更新的答案。
Mohit Kumar Arora's

直到不工作。
Sarfaraj Sipai

谢谢@MohitKumarArora-保存了我的一天。上面的解决方案就像魅力一样工作
Abid Malik

6

使用以下代码获取.phtml文件上的媒体路径。

$this->getUrl('pub/media');

由Objectmanager

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

3
它返回浏览器的URL路径。我需要媒体的文件夹路径
Rita Jose

6

尝试使用StoreManagerInterface来获取它

use Magento\Store\Model\StoreManagerInterface;
protected $storeManager;

public function __construct(
    StoreManagerInterface $storeManager,
)
{
    $this->storeManager = $storeManager;
}

现在使用获取媒体网址

 $mediaUrl = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);

无法正常工作..
Sarfaraj Sipai
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.