Call to a member function get() on null
-
10-04-2021 - |
Question
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?
Solution
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();
OTHER TIPS
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/
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