如何获取Magento 2安全的基本URL?

如何获取Magento 2安全的基本URL?,magento,magento2,Magento,Magento2,我使用下面的代码来获取magento 2中的基本url $storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface'); echo $baseurl = $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB); 让我知道如何获取安全的基本url。您的代码是正确的,

我使用下面的代码来获取magento 2中的基本url

$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
echo $baseurl = $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB); 

让我知道如何获取安全的基本url。

您的代码是正确的,只需使用
getBaseUrl()
方法传递第二个参数boolean
true
,默认为空,使用如下代码:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
echo $baseurl = $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB); 
echo $secureBaseUrl = $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB,true); 
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
echo $secureBaseUrl = $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB,true);