0

I am new to magento 2 and i have tried to get the store id. Here is my code simplifyed:

class Mage{  
   private static $_objectManger;
   public  function __construct(
      \Magento\Framework\ObjectManagerInterface $objectmanager
   )
   {
      self::$_objectManager = $objectmanager;
   }
   public static function  getStore(){
      return self::$_objectManger->get("\Magento\Store\Model\StoreManagerInterface");
   }
}

And in my .phtml file i try to call it like this:

$store_id= Mage::getStore()->getStoreId();
echo $store_id;

and i get this error message exactly at this line:

return self::$_objectManger->get("\Magento\Store\Model\StoreManagerInterface");

So why does this doesnt work?

2
  • here i show you full code
    – Msquare
    Feb 21, 2020 at 10:25
  • @Msquare is right. i used this code in my module. it's works
    – user83874
    Feb 21, 2020 at 12:13

3 Answers 3

1

In your Module Block file add below code.

<?php
namespace Vendor\Module\Block;
class Module 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);
    }

    /**
     * Get store identifier
     *
     * @return  int
     */
    public function getStoreId()
    {
        return $this->_storeManager->getStore()->getId();
    }
}

In your PHTML file

echo $block->getStoreId();
14
  • where is the block file Feb 21, 2020 at 8:58
  • You can create it in your module. Feb 21, 2020 at 8:59
  • how should i name the php file? Feb 21, 2020 at 9:00
  • Have you created a module? Feb 21, 2020 at 9:00
  • My vendor name is Steiner and my Module is called All and it has currently these folders: etc view composer.json and registration.php and i have created an folder named block Feb 21, 2020 at 9:02
0

In magento 2 you should never use object manager to instantiate new objects. Always use dependency injection instantiating your object in the constructor.

In your phtml file you have magento 1 code.

This shoud look something like this:

BLOCK:

<?php
/**
 * @category    Magento 2
 * @author      Mattia Kozianowski
 */
declare(strict_types=1);

namespace Vendor\Module\Block\MyBlock;

use Magento\Framework\View\Element\Template;

/**
 * Class MyClass
 *
 * @package Vendor\Module;
 */
class MyClass extends Template
{
    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $storeManager;

    /**
     * MyClass constructor.
     *
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param array $data
     */
    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        Template\Context $context,
        array $data = []
    )
    {
        parent::__construct($context, $data);
        $this->storeManager = $storeManager;
    }

    /**
     * @return \Magento\Store\Api\Data\StoreInterface
     * @throws \Magento\Framework\Exception\NoSuchEntityException
     */
    public function getStore()
    {
        return $this->storeManager->getStore();
    }
}

Then place your block specifying the template (phtml) using layout xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!--
/**
 * @category    Magento 2
 * @author      Mattia Kozianowski
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="checkout"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <block class="Vendor\Module\Block\MyBlock\MyClass" template="Vendor_Module::myfile.phtml"/>
    </body>
</page>

Call your method in phtml:

<?php
/**
 * @category    Magento 2
 * @author      Mattia Kozianowski
 */

/** @var $block \Vendor\Module\Block\MyBlock\MyClass */
?>

<h1><?= $block->getStore()->getName()?></h1>

You can find some more information here: https://www.cloudways.com/blog/magento-2-layouts-blocks-templates/

0

This is tested code

Vendor/Siteinfo/etc/registration.xml

registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Siteinfo',
    __DIR__
);

Vendor/Siteinfo/etc/module.xml

module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_Siteinfo" setup_version="1.0.0">
    </module>
</config>

Vendor/Siteinfo/etc/frontend/routes.xml

routes.xml

<?xml version="1.0" ?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route frontName="siteinfo" id="siteinfo">
            <module name="Vendor_Siteinfo"/>
        </route>
    </router>
</config>

Vendor/Siteinfo/Block/Siteinfo.php

Siteinfo.php

<?php

namespace Vendor\Siteinfo\Block;

class Siteinfo extends \Magento\Framework\View\Element\Template
{
    protected $_storeManager;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Store\Model\StoreManagerInterface $storeManager
    ) {
        $this->_storeManager = $storeManager;
        parent::__construct($context);
    }
    public function getStoreInfo()
    {
        echo $this->_storeManager->getStore()->getId() . '<br />';
        // by default: URL_TYPE_LINK is returned
        echo $this->_storeManager->getStore()->getBaseUrl() . '<br />';
        echo $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB) . '<br />';
        echo $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_DIRECT_LINK) . '<br />';
        echo $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . '<br />';
        echo $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_STATIC) . '<br />';
        echo $this->_storeManager->getStore()->getUrl('product/33') . '<br />';
        echo $this->_storeManager->getStore()->getCurrentUrl(false) . '<br />';
        echo $this->_storeManager->getStore()->getBaseMediaDir() . '<br />';
        echo $this->_storeManager->getStore()->getBaseStaticDir() . '<br />';
    }
}

Vendor/Siteinfo/view/frontend/templates/storename.phtml

storename.phtml

<h2>Store Info </h2>
<?php $block->getStoreInfo(); ?>

Vendor/Siteinfo/Controller/Storeinfo/Storeinfo.php

Storeinfo.php

<?php

namespace Vendor\Siteinfo\Controller\Storeinfo;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;

class Storeinfo extends \Magento\Framework\App\Action\Action
{

    protected $resultPageFactory;

    public function __construct(
        Context $context,
        PageFactory $resultPageFactory,
    ) {
        $this->resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }

    public function execute()
    {
        return $this->resultPageFactory->create();
    }
}

Vendor/Siteinfo/view/frontend/layout/siteinfo_storeinfo_storeinfo.xml

siteinfo_storeinfo_storeinfo.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <title>SiteInfo</title>
    </head>
      <body>
        <block class="Vendor/Siteinfo/Block/Siteinfo" name="storelocator_index_index" template="Vendor_Siteinfo::storename.phtml" />
      </body>
</page>

run magento command

php bin/magento s:up
php bin/magento s:s:d -f
php bin/magento c:c
php bin/magento c:f

now you check it into your url

http://example/m234/siteinfo/storeinfo/storeinfo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.