How to check url is secured https, ssl in Magento 2

If you are finding the way to check url is secured https, ssl when Magento 2 platform is applied for your store, this topic is the right place to give you the perfect solution

If you are finding the best answer to the question How to check url is secured https, SSL when Magento 2 platform is applied for your store, this topic is the right place to give you the perfect solution. How about the overview of the topic? Mageplaza team assists you to check if URL is secured https, ssl through the code snippets.

Overview of checking the url is secured https, ssl in Magento 2

  • Step 1: Declare in Mageplaza_HelloWorld
  • Step 2: Declare function in template .phtml file

Step 1: Declare in Mageplaza_HelloWorld

You will use a block class of the module Mageplaza_HelloWorld, then possibly inject the object of StoreManagerInterface in the constructor of the module’s block class.

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();
    }    
}
?>

You can see more functions in vendor/magento/module-store/Model/Store.php.

Step 2: Declare function in template .phtml file

Run the below function in your template .phtml file

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

All above steps are the necessary things you need to do. If that you have any queries about the article or any questions in general, use the comment section below!

Related Podst

Enjoyed the tutorial? Spread it to your friends!

magento-2-tutorial
check
url
secured
https
ssl

Sam Thomas
Sam Thomas

CEO and Founder of Mageplaza. Pursueing a simple and healthy lifestyle. A friend, a husband and a dad of two children, a trainer and a wanna-be influencer. He is a big fan of sports and travel, also.

People also searched for

  • magento 2 check url is secured https ssl
  • check url is secured https ssl in magento 2
  • how to check url is secured https ssl
  • how to check url is secured https ssl in magento 2
  • 2.2.x, 2.3.x