php .phtml 文件中的 Magento2 媒体路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35700055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Magento2 Media path in .phtml file
提问by Mitul
I have tried lots of option in magento2 to find the media path URL
我在 magento2 中尝试了很多选项来查找媒体路径 URL
How can i find the media path URL in template.
如何在模板中找到媒体路径 URL。
I have try
我试过了
$object_manager = Magento\Core\Model\ObjectManager::getInstance();
$dir = $object_manager->get('Magento\App\Dir');
$mediaUrl = $dir->getDir(\Magento\App\Dir::MEDIA);
But it give me error class not found. I have search and found the one solution to create function in block file but there are so many places i want to use the media path in my design
但它给了我找不到错误类。我已经搜索并找到了在块文件中创建函数的一种解决方案,但是我想在我的设计中使用媒体路径的地方太多了
Thanks in advance.
提前致谢。
回答by 1984
This is how you get the media path from inside a block or template.
这是从块或模板内部获取媒体路径的方式。
$this->getUrl('pub/media')
回答by Arvind Bhardwaj
The correct way to get media in PHTML is:
在 PHTML 中获取媒体的正确方法是:
$block->getViewFileUrl('images/myimage.png');
回答by N. Karthic Kannan
Usage of Object Manager is discouraged as per Magento's coding standards. In a template file, we can get the media URL using the following code:
根据 Magento 的编码标准,不鼓励使用对象管理器。在模板文件中,我们可以使用以下代码获取媒体 URL:
$this->helper('\Magento\Cms\Helper\Wysiwyg\Images')->getBaseUrl()
As per the Magento's recommendation, the document root of your website should be outside pub
directory during development and it should be inside pub
directory when moved to production.
根据 Magento 的建议,您网站的文档根pub
目录在开发过程中应位于pub
目录外,而在移至生产环境时应位于目录内。
Hence we cannot hard code the pub
directory while getting media URL in the template files. The above code will give you the correct media URL irrespective of your document root location.
因此,我们无法pub
在模板文件中获取媒体 URL 时对目录进行硬编码。无论您的文档根位置如何,以上代码都会为您提供正确的媒体 URL。
回答by Ipsita Rout
//Case 1: $objectManager outside magento
use Magento\Framework\App\Bootstrap;
include('your-path-to-/app/bootstrap.php');
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
//Case 2: $objectManager inside magento
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$mediaDir = $objectManager->get('Magento\Framework\App\Filesystem\DirectoryList')->getPath('media');
$mediaUrl = $objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
// Case 3: Inside model
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Framework\App\Filesystem\DirectoryList $directory_list,
\Magento\Store\Model\StoreManagerInterface $url,
array $data = []) {
parent::__construct($context, $data);
$this->directory_list = $directory_list;
$this->url = $url;
}
$this->directory_list->getRoot();//root folder path
$this->directory_list->getPath('media');//media folder path
$this->url->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
It worked fine for me.
它对我来说很好。
回答by swathi_sri
Try to get it by using StoreManagerInterface
尝试使用 StoreManagerInterface 获取它
use Magento\Store\Model\StoreManagerInterface;
protected $storeManager;
public function __construct(
StoreManagerInterface $storeManager,
)
{
$this->storeManager = $storeManager;
}
Now get media url using
现在使用获取媒体网址
$mediaUrl = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
回答by Altravista
in your block inject StoreManagerInterface
在您的块中注入 StoreManagerInterface
namespace Altravista\Carousel\Block;
use Magento\Catalog\Block\Product\ImageBuilder;
class Product extends \Magento\Framework\View\Element\Template
{
public $_storeManager;
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Store\Model\StoreManagerInterface $storeManager,
array $data = []
)
{
$this->_storeManager = $storeManager;
parent::__construct($context, $data);
}
public function getMediaUrl(){
return $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
}
}
in your phtml call the block
在你的 phtml 中调用块
$productBlock = $block->getLayout()->createBlock('Altravista\Carousel\Block\Product');
and the method
和方法
$imageUrl = $media_url. 'catalog/product' . $product->getImage();
回答by hardik solanki
Give it try with below code:
尝试使用以下代码:
$object_manager = \Magento\Framework\App\ObjectManager::getInstance();
$dir = $object_manager->get('Magento\App\Dir');
$mediaUrl = $dir->getDir(\Magento\App\Dir::MEDIA);
回答by Emizen Tech
There are two way's to get the media Path in PHTML file:
有两种方法可以在 PHTML 文件中获取媒体路径:
First Way
第一种方式
$om = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $om->get('\Magento\Store\Model\StoreManagerInterface');
var_dump($storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA));
Second Way
第二种方式
index.php
索引.php
contains:
包含:
define('MAGENTO_ROOT', getcwd());
One could do something like:
一个人可以做这样的事情:
$mediaPath = MAGENTO_ROOT.'/pub/media/';
回答by Manish Joy
Well, in my case, this worked:
好吧,就我而言,这有效:
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get('Magento\Store\Model\StoreManagerInterface');
$store = $storeManager->getStore();
$mediaUrl = $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
?>
回答by Sreenath
$baseurl=$block->getUrl();
$baseurl=$block->getUrl();
Returns your baseurl inside phtml.
在 phtml 中返回您的 baseurl。