1

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

2 Answers 2

4

cacheable=false in layout file did the trick.

<block class="YS\India\Block\Header" template="header/top_header.phtml" cacheable="false">

2
  • The reason behind this is, that cacheable blocks are "Depersonalized" to avoid that customer data are stored in the Magento cache and presented to other customers. The solution you mentioned is correct, but you have to recognize, that the page cache will not work anymore for that particular page. Commented Jul 7, 2016 at 12:06
  • 1
    Using cacheable=false nowadays is a bad practice and it should not be recommended. Instead, any block should be made Full Page Cache compatible (cacheable=true). If you want to use customer-specific details anyway, use the procedure of Private Content to do so: devdocs.magento.com/guides/v2.1/config-guide/cache/… Commented Jul 15, 2017 at 10:25
-1

If you can't access session by instance of Magento\Customer\Model\Session Try instance Magento\Framework\App\Http\Context
Sample:

public function __construct(
    \Magento\Framework\App\Http\Context $httpContext
){
    $this->httpContext = $httpContext;
}

public function getCustomerSession()
{
    $this->httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH)->getSession();
}
2
  • Hi mrtuvn, getValue(..) returns boolean value. Commented Jan 6, 2016 at 10:40
  • 2
    You can use the http context to get the information if the customer is logged in or not. You cannot get detailed customer information that way. Please do not try to load the customer object in your objects by using the customer_id in the session. Commented Jul 7, 2016 at 12:08

Your Answer

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

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