I'm not able to access customer session in my block. Session does not return customer. Here is my code:
<?php
namespace YS\India\Block;
use Magento\Customer\Model\Session;
use Magento\Framework\View\Element\Template\Context;
use Magento\Framework\View\Element\Template;
class Header
extends Template
{
protected $session;
protected $customerRepository;
public function __construct(
Context $context,
Session $session,
array $data = []
)
{
$this->session = $session;
parent::__construct($context, $data);
}
public function getBaseUrl(){
return $this->_storeManager->getStore()->getBaseUrl();
}
public function isCustomerLogged(){
return $this->session->isLoggedIn();
}
public function getLoggedCustomerName(){
return $this->session->getCustomer()->getFirstname();
}
}
Then I created a test controller:
<?php
namespace YS\India\Controller\Auth;
use Magento\Framework\App\Action\Context;
use Magento\Customer\Model\Session;
use Magento\Customer\Controller\AbstractAccount;
class Test extends AbstractAccount
{
protected $session;
public function __construct(
Context $context,
Session $customerSession
)
{
$this->session = $customerSession;
parent::__construct($context);
}
public function execute()
{
echo $this->session->isLoggedIn() ? "yes" : "no";
echo "<br/>" . $this->session->getCustomer()->getFirstname();
}
}
This controller works fine and gives the first name.
Please help what is I'm missing in block
Thanks