3

below is my block file code

class View extends \Magento\Framework\View\Element\Template 
{ 
protected $_registry;
protected $_customerSession; 
protected $_customer; 
public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Framework\Registry $registry,
    \Magento\Customer\Model\Session $customerSession, 
    \Magento\Customer\Model\Customer $customer      
) {
    $this->_registry = $registry;
    $this->_customerSession = $customerSession; 
    $this->_customer = $customer; 
    parent::__construct($context);
}

public function getUser()
{
    if (!$this->_loadValidUser()) {
        return;
    }
    return $this->registry->registry('current_user');
}   

protected function _loadValidUser($userId = null)
{
     $userId = (int) $this->getRequest()->getParam('user_id');
    try {
        $user = $this->_customer->load($userId);
        $this->registry->register('current_user', $user);            
        return $user;
    }catch (Exception $e) {
       print_r($e->getMessage());
         return false;
    }

}

}

What is the mistake in above construction? I am getting the error like

PHP Fatal error: Uncaught TypeError: Argument 1 passed to Magento\Framework\View\Element\Template

8

2 Answers 2

1

Use this code in your block file :

protected $_customerSession;

public function __construct(
    ......
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Customer\Model\SessionFactory $customerSession,
    array $data = []
    ......
)
{
    ......
    $this->_customerSession = $customerSession->create();
    parent::__construct($context, $data);
    ......
}

public function getCustomerData() {
    if ($this->_customerSession->isLoggedIn()) {
        return $this->_customerSession->getCustomerData();
    }
    return false;
}

and display in your phtml file :

$customerData = $block->getCustomerData();
echo "<pre/>";
print_r($customerData);
11
  • I am getting userId in param and i need to register that user instead of current logged in customer. Aug 7, 2018 at 5:11
  • Sorry ? I'm not getting exactly what you want to explain. Aug 7, 2018 at 5:41
  • can you explain in some more detail? Aug 7, 2018 at 5:41
  • In my code i used $userId = (int) $this->getRequest()->getParam('user_id'); to get parameter. from the parameter i am registering customer data. parameter is customer id, Aug 7, 2018 at 5:44
  • Try to use this code $this->_registry->register('current_user', $user); in _loadValidUser function. Aug 7, 2018 at 5:47
2

Try this:

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Framework\Registry $registry,
    \Magento\Customer\Model\Session $customerSession,
    \Magento\Customer\Model\Customer $customer       
){
    $this->_registry = $registry;
    $this->_customerSession = $customerSession;
    $this->_customer = $customer;
    parent::__construct($context);
}

after that changes please run below commands

php bin/magento cache:flush and php bin/magento setup:di:compile

1
  • hi kunj, used above code no error but i am not getting current customer data. Aug 6, 2018 at 12:22

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.