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 way to check if a URL is secure or not, this article is for you. Inill instruct you on to check the security of a URL via snippets.
2 Steps to check if 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 />';
Related Podst
- Check if current url is homepage in Magento 2
- Get base url, current url in Magento 2
- Get product by id and sku in Magento
- Magento 2 Tutorials for Beginner
- Magento 2 Services
All above steps are the necessary things you need to do. If you have any issue while following this tutorial, feel free to let me know in the comment.
Thank you for reading!
Enjoyed the tutorial? Spread it to your friends!
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 an influencer wannabe. He is a big fan of sports and travel, also.
Featured Extensions
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, 2.4.x