Skip to content

Instantly share code, notes, and snippets.

@magevision
Created July 24, 2018 10:05
Show Gist options
  • Save magevision/5b926656251e069224375b1c34604832 to your computer and use it in GitHub Desktop.
Save magevision/5b926656251e069224375b1c34604832 to your computer and use it in GitHub Desktop.
Get the Base Url
<?php
/**
* MageVision Blog29
*
* @category MageVision
* @package MageVision_Blog29
* @author MageVision Team
* @copyright Copyright (c) 2018 MageVision (https://www.magevision.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
namespace MageVision\Blog29\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Store\Model\StoreManagerInterface;
class Data extends AbstractHelper
{
/**
* @var StoreManagerInterface
*/
protected $storeManager;
/**
* @param Context $context
* @param StoreManagerInterface $storeManager
*/
public function __construct(
Context $context,
StoreManagerInterface $storeManager
) {
parent::__construct($context);
$this->storeManager = $storeManager;
}
/**
* Get the base url
*
* @return string|null
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getBaseUrl()
{
//examples
$this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);
$this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_DIRECT_LINK);
$this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_JS);
$this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
$this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_STATIC);
//default link type \Magento\Framework\UrlInterface::URL_TYPE_LINK
return $this->storeManager->getStore()->getBaseUrl();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment