magento2 获取base url , media url 和 static url

redmaomail 2024-07-24 18:14 阅读数 63 #Magento

红帽云邮外贸主机

在Magento2开发过程中经常会用到Base Url,Media Url,Static Url,下面就讲下在Magento2的不同文件中调用Base Url,Media Url,Static Url

在phtml文件中获取Base Url,Media Url,Static Url

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
/* Get base url with index When index.php enable at Url */
$baseUrl = $storeManager->getStore()->getBaseUrl();
/* Result  http://www.example.com/ or http://www.example.com/index.php  depends SEo rewite settng*/
/* it always give the URL without Index.php either index.php enable for Url */ 
$baseUrlWithOutIndexPhp = $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);
// $baseUrl  result = http://example.com/

/* It is give url with index.php whenever  seo rewrite not enable mean when  Index.php show at Url  */

$linkUrl= $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_LINK);
 
 /* $linkUrl  result = http://example.com/index.php/
 		 whenever  seo rewrite not enable mean when  Index.php show at Url
		
		but when  SEO Rewrite enabled
		
		Then it show result = http://example.com/
 
 */

$statiContenteUrl =$storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_STATIC);
/* Result $statiContenteUrl = http://example.com/pub/static/version1507042743/
 	In this part  version1507042743 is 1507042743 static content version which dynamcically created
*/

$mediaUrl =  $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
/* Result $mediaUrl = http://example.com/pub/media/

*/

直接使用对象管理器不是一个好主意

如果在您的Block/helper/controller/Model中有Store Manager Object类,你就可以用下面的代码获取Base Url,Media Url,Static Url,注意,你讲通过Magento\Store\Model\StoreManagerInterface注入类获取商店管理器对象

获取Base Url

/* it always give the URL  Index.php either index.php enable for Url */ 
/*  result   is http://example.com/  or http://example.com/index.php depends Magento Setting SEO Rewrite  */
$this->_storeManager->getStore()->getBaseUrl()

你将得到如下结果

http://www.example.com (如果Seo rewrite 开启) 和 http://www.example.com/index.php (如果Seo rewrite 未开启)

如果你想获取的Base Url不带index.php,用下面的代码

$this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB)

获取Media Url

$this->_storeManager->getStore()->getBaseUrl()

获取链接 Url

 /* $linkUrl  result = http://example.com/index.php/ whenever  seo rewrite not enable mean when  Index.php show at Url
		but when  SEO Rewrite enabled Then it show result = http://example.com/
 */
  $this->_storeManager->getStore()
               ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_LINK);

获取Static Url

/* Result $statiContenteUrl = http://example.com/pub/static/version1507042743/
 	In this part  version1507042743 is 1507042743 static content version which dynamcically created

$this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_STATIC);

在Magento1中你可以用Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB))来获取Base Url,Magento1获取Base Url , Skin Url , Media Url , Js Url , Store Url 和 Current Url链接

在Magento2中,没有使用工厂模式,所以没有像Mage :: getBaseUrl()的代码,你可以使用StoreManager object,然后用$this->_storeManager->getStore()->getBaseUrl()你就可以获得Base Url了。

Magento2中,使用接口、注入类,你应该注入

Magento\Store\Model\StoreManagerInterface

在Model,Resource model,Helper类中获取Base URL。

在下面的示例类中,我已经为我的类注入了StoreManagerInterface

<?php
namespace {MyClassNameSpace};
class {ClassName} 
{
    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManager;
 
    public function __construct(
		 ......
        \Magento\Store\Model\StoreManagerInterface $storeManager
			 .......
    ) {
 		.....
        $this->_storeManager = $storeManager;
 		.....
    }
 
    public function mygStoreBaseUrl(){
		
		$statiContenteUrl = $this->_storeManager->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_STATIC);
		$mediaUrl = $this->_storeManager->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
		$linkUrl = $this->_storeManager->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_LINK);
		$baseUrlwithoutIndexPhp = $this->_storeManager->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);

 		return $this->_storeManager->getStore()
					->getBaseUrl();
					
    }
    public function myStoreUrl(){
 
 		return $this->_storeManager->getStore()->getBaseUrl()
    }
    
}

请注意:如果您使用了任何块类扩展基本块模板

\Magento\Framework\View\Element\Template

应该避免为该情况注入商店管理器接口,因为extends类已经有商店管理器对象

 \Magento\Store\Model\StoreManagerInterface

尝试下面的代码来获取magento2 BaseURL

$this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB)

如何通过对象管理器获取magento2 Base URL 使用对象管理器获取magento2 Base URL然后尝试下面:

$this->_objectManager->get('Magento\Store\Model\StoreManagerInterface')
            ->getStore($storeId)
            ->getBaseUrl();

假设您的类具有对象管理器。

如果没有对象管理器,那么试试这个:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$storeManager->getStore()->getBaseUrl();

但是这种类型的对象管理器调用真的很糟糕。


红帽云邮外贸主机

分享到:
版权声明:本站内容源自互联网,如有内容侵犯了你的权益,请联系删除相关内容。
    红帽云邮外贸主机
热门
    红帽云邮外贸主机
    红帽云邮外贸主机