Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/sbboke.com/wp-content/plugins/wordpress-seo/src/integrations/front-end-integration.php:409) in /www/wwwroot/sbboke.com/wp-content/themes/pacify/content-single.php on line 5

magento2 获取基本URL,媒体URL和静态URL

magento2获取基本URL,媒体URL和静态URL。每当您在magento2上工作时,   很多时候您都需要获取基本URL,媒体URL和静态URL。从这个博客中,您基本上是在Magento2.Magento2中建立URL,媒体URL,静态内容URL,使用界面

/ *这是接口类* /
Magento \ Store \ Model \ StoreManagerInterface

此类具有一个Store()  函数  ,用于提供商店数据。

之后,在 Store()上  使用  getBaseUrl()可以获取基本URL,也可以获取Media URL。

/ *使用直接对象管理器* / 
$ objectManager = \Magento\Framework\App\ObjectManager :: getInstance();
/ *获取商店经理* / 
$ storeManager = $ objectManager-> get('\ Magento \ Store \ Model \ StoreManagerInterface');
//基本网址 
$ baseUrl = $ storeManager-> getStore()-> getBaseUrl();

//媒体网址 
$ mediaUrl = $ storeManager-> getStore()-> getBaseUrl(\ Magento \ Framework \ UrlInterface :: URL_TYPE_MEDIA);
//静态内容网址 

$ statiContenteUrl = $ storeManager-> getStore()-> getBaseUrl(\ Magento \ Framework \ UrlInterface :: URL_TYPE_STATIC);

其中  $ storeManagerMagento \ Store \ Model \ StoreManagerInterface的实例  

要获取媒体网址,请使用以下代码:

$ mediaUrl = $ storeManager-> getStore()-> getBaseUrl(\ Magento \ Framework \ UrlInterface :: URL_TYPE_MEDIA);

获取静态内容网址

$ statiContenteUrl = $ storeManager-> getStore()-> getBaseUrl(\ Magento \ Framework \ UrlInterface :: URL_TYPE_STATIC);

但是强烈建议不要使用对象管理器。在类的__construct函数上使用接口注入。如下例所示:

     / * @var \ Magento \ Store \ Model \ StoreManagerInterface
     * /
    protected $_storeManager;
 
    public __construct(
 	......
        \ Magento \ Store \ Model \ StoreManagerInterface $ storeManager
	 .......
    ){
	 .....
        $ this-> _ storeManager = $ storeManager;
 	.....
    }
 
    public myStoreBaseUrls(){
 	$ storeManager = $ this-> _ storeManager;
 	$ statiContenteUrl = $ storeManager-> getBaseUrl(\ Magento \ Framework \ UrlInterface :: URL_TYPE_STATIC);
	$ mediaUrl = $ storeManager-> getBaseUrl(\ Magento \ Framework \ UrlInterface :: URL_TYPE_MEDIA);
 	$ linkUrl = $ storeManager-> getBaseUrl(\ Magento \ Framework \ UrlInterface :: URL_TYPE_LINK);
 	$ baseUrlwithoutIndexPhp = $storeManager-> getBaseUrl(\ Magento \ Framework \ UrlInterface :: URL_TYPE_WEB);
  	return $this-> _ storeManager-> getStore()-> getBaseUrl();
     }
    public myStoreUrl(){
 	return $this-> _ storeManager-> getStore()-> getBaseUrl()
    }

想要使用Out Index.php获取基本网址,然后在下面的代码中进行操作:

$baseUrlWithOutIndexPhp = $storeManager->getStore()
->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);

上面的代码总是给没有Index.php的URL或是否在管理员处启用了Seo重写,这与该设置无关。

哪里,

$linkUrl= $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_LINK);

相关文章

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