Magento 2检查网址是https,ssl

如果您正在找到问题的最佳答案 如何检查网址是否受到保护https,当您的商店应用Magento 2平台时SSL,检查URL是否受到保护https,ssl是否通过代码段。

在Magento 2中检查URL的概述是安全的https,ssl

  • 第1步:声明 Mageplaza_HelloWorld
  • 第2步:在模板.phtml文件中声明函数

第1步:声明 Mageplaza_HelloWorld

您将使用模块的块类Mageplaza_HelloWorld,然后可能StoreManagerInterface在模块的块类的构造函数中注入对象。

app/code/Mageplaza/HelloWorld/Block/HelloWorld.php

<?php
namespace Mageplaza\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

第2步:在模板.phtml文件中声明函数

在模板.phtml文件中运行以下函数

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

以上所有步骤都是您需要做的事情。如果您对文章或任何问题有任何疑问,请使用下面的评论部分!

赞(0)
微信
支付宝
微信二维码图片

微信扫描二维码打赏

支付宝二维码图片

支付宝扫描二维码打赏

相关文章

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