在你的块中注入 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);
}
}
在你的 phtml 中调用块
$productBlock = $block->getLayout()->createBlock('Altravista\Carousel\Block\Product');
和方法
$imageUrl = $media_url. 'catalog/product' . $product->getImage();