在Magento2中,我们如果想要获得一个url链接,首先我们需要来实例化\Magento\Framework\App\ObjectManager类来获得当前店铺:


    $_objectManager = \Magento\Framework\App\ObjectManager::getInstance(); //实例化“\Magento\Framework\App\ObjectManager”类
    $storeManager = $_objectManager->get('Magento\Store\Model\StoreManagerInterface'); 
    $currentStore = $storeManager->getStore();

实例化之后,就可以轻松的使用$currentStoregetBaseUrl()来获得店铺的url了,获得当前店铺url:

        $baseUrl = $currentStore->getBaseUrl();
        /*输出链接如下:*/
        /*http://magento2.magease.com*/

获得MEDIA url:

        $mediaUrl = $currentStore->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
        /*输出MEDIA链接如下:*/
        /*http://magento2.magease.com/pub/media*/

获得LINK url:

        $linkUrl = $currentStore->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_LINK);
        /*输出LINK链接如下:*/
        /*http://magento2.magease.com*/