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

Magento 2:检查当前URL和前端URL是否安全(https)

本文介绍了如何在Magento 2中检查当前URL和前端UR1是否安全(https)。

以下是我的自定义模块(Chapagain_HelloWorld)的块类。我在模块的模块类的构造函数中注入了StoreManagerInterface对象。

app / code / Chapagain / HelloWorld / Block / HelloWorld.php

<?php
namespace Chapagain\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
    protected $_storeManager;    
    
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        \Magento\Store\Model\StoreManagerInterface $storeManager,        
        array $data = []
    )
    {        
        $this->_storeManager = $storeManager;        
        parent::__construct($context, $data);
    }
    
    /**
     * Check if frontend URL is secure
     *
     * @return boolean
     */
    public function isFrontUrlSecure()
    {
        return $this->_storeManager->getStore()->isFrontUrlSecure();
    }
    
    /**
     * Check if current requested URL is secure
     *
     * @return boolean
     */    
    public function isCurrentlySecure()
    {
        return $this->_storeManager->getStore()->isCurrentlySecure();
    }    
}
?>

请参阅vendor / magento / module-store / Model / Store.php中的更多功能。

现在,我们可以在模板(.phtml)文件中使用以上功能。

var_dump($block->isFrontUrlSecure()) . '<br />';
var_dump($block->isCurrentlySecure()) . '<br />';

使用对象管理器

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

希望这可以帮助。谢谢。

相关文章

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