Sometimes, during Magento 2 customization you need to get the store Information programmatically. You may need to get a current Store ID, Store Code, Name, Website ID, or current URL. To retrieve this data use the singleton instance of the following class:


For example, you can include it in your class constructor and then call:


namespace \MyCompany\ExampleModule\Model;

class Example
private $storeManager;
public function __construct( ... \Magento\Store\Model\StoreManagerInterface $storeManager, ... ) {
... $this->storeManager = $storeManager; ... }     /**
    * Examples
    public function execute()
    /* Get Current Store ID */
      $storeId = $this->storeManager->getStore()->getId();

    /* Get Current Store Code */
       $storeCode = $this->storeManager->getStore()->getCode();

    /* Get Current Store Name */
      $storeName = $this->storeManager->getStore()->getName();

    /* Get Current Store Status */
      $isActive = $this->storeManager->getStore()->isActive();

    /* Get Current Website ID */
$websiteId = $this->storeManager->getStore()->getWebsiteId();

/* Get Current URL */
$websiteId = $this->storeManager->getStore()->getCurrentUrl();

    /* Get Login Page URL */
$loginPageUrl = $this->storeManager->getStore()->getUrl('customer/account/login');

    /* Get Code of Store With ID 2 */
$secondStoreCode = $this->storeManager->getStore(2)->getCode();
    } }

You can use the object manager to quickly get the StoreManager object for a test, as in the example bellow:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get(\Magento\Store\Model\StoreManagerInterface::class);

Attention! If you want to use the StoreManager inside a block class and the block is extended from \Magento\Framework\View\Element\Template, then StoreManager is available for you out of the box through protected property $this->_storeManager. Example:


namespace \MyCompany\ExampleModule\Block;

class Example extends \Magento\Framework\View\Element\Template

    public function getCurrentStoreId()
    /* Get Current Store ID */
      return $this->_storeManager->getStore()->getId();     } }