Magento 2获得基本网址,当前网址

如何获得Magento 2中的基本网址和当前网址。让我们通过运行代码片段来完成以下两个步骤。

要在Magento 2中获取基本网址和当前网址:

  • 第1步:声明 Mageplaza_HelloWorld
  • 第2步:获取模板(.phtml)文件中的当前URL和基本URL
  • 第3步:刷新缓存并检查结果

第1步:在Mageplaza_HelloWorld中声明

您将使用一个块类模块Mageplaza_HelloWorld,然后可能注入的对象StoreManagerInterface,并UrlInterface在模块的块类的构造函数。您将使用以下类中的两个函数:getStoreManagerData()getUrlInterfaceData()

  • getStoreManagerData()函数中,您将使用object StoreManagerInterface来获取基数和当前URL。
  • getUrlInterfaceData()函数中,您将使用object UrlInterface来获取基数和当前URL

打开app/code/Mageplaza/HelloWorld/Block/HelloWorld.php类并运行代码:

<?php
namespace Mageplaza\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
        protected $_storeManager;
        protected $_urlInterface;
 
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\UrlInterface $urlInterface,    
        array $data = []
    )
    {        
        $this->_storeManager = $storeManager;
        $this->_urlInterface = $urlInterface;
        parent::__construct($context, $data);
    }
    
    public function _prepareLayout()
    {
        return parent::_prepareLayout();
    }
    
    /**
     * Prining URLs using StoreManagerInterface
     */
    public function getStoreManagerData()
    {    
        echo $this->_storeManager->getStore()->getId() . '<br />';
        
        // by default: URL_TYPE_LINK is returned
        echo $this->_storeManager->getStore()->getBaseUrl() . '<br />';        
        
        echo $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB) . '<br />';
        echo $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_DIRECT_LINK) . '<br />';
        echo $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . '<br />';
        echo $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_STATIC) . '<br />';
        
        echo $this->_storeManager->getStore()->getUrl('product/33') . '<br />';
        
        echo $this->_storeManager->getStore()->getCurrentUrl(false) . '<br />';
            
        echo $this->_storeManager->getStore()->getBaseMediaDir() . '<br />';
            
        echo $this->_storeManager->getStore()->getBaseStaticDir() . '<br />';    
    }
    
    /**
     * Prining URLs using URLInterface
     */
    public function getUrlInterfaceData()
    {
        echo $this->_urlInterface->getCurrentUrl() . '<br />';
        
        echo $this->_urlInterface->getUrl() . '<br />';
        
        echo $this->_urlInterface->getUrl('helloworld/general/enabled') . '<br />';
        
        echo $this->_urlInterface->getBaseUrl() . '<br />';
    }
    
}
?>

查看更多功能

- MAGENTO_ROOT / vendor / magento / module-store / Model / Store.php - MAGENTO_ROOT / vendor / magento / framework / Url.php

第2步:在模板文件中获取基本URL和当前URL

请使用以下代码:

    <?php echo $block->getUrl('hello/test'); ?>
    <?php echo $block->getBaseUrl(); ?>

第3步:刷新缓存并检查结果

刷新Magento缓存并检查结果。

这是获取Magento 2中的基本网址和当前网址的指南。如果您对文章或任何问题有任何疑问,请使用下面的评论部分!

相关文章

0 0 投票数
文章评分
订阅评论
提醒
0 评论
内联反馈
查看所有评论