I'm trying to inject customer session in my block:
use Magento\Framework\View\Element\Template;
use Magento\Customer\Model\Session;
class Header
extends Template
{
protected $session;
public function __construct(
Template\Context $context,
Session $customerSession
)
{
$this->session = $customerSession;
parent::__construct($context);
}
public function getBaseUrl(){
return $this->_storeManager->getStore()->getBaseUrl();
}
public function isCustomerLogged()
{
if($this->session->isLoggedIn())
return "yes";
else
return "no";
}
public function getLoggedCustomerName(){
return $this->session->getCustomer()->getFirstName();
}
}
Updated question
$this->session->isLoggedIn() always returns false whereas in controller, it returns true. The Context namespaces are different for both:
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Element\Template\Context;
Please help