i am trying to get customer id of a logged in customer but its does not seems to be working
in my block
<?php
namespace Vendor\Module\Block;
use Magento\Framework\View\Element\Template;
use Magento\Backend\Block\Template\Context;
class Rfq extends \Magento\Framework\View\Element\Template
{
protected $_customerSession;
public function __construct(Context $context,
\Magento\Customer\Model\Session $customerSession,
array $data = [])
{
parent::__construct($context, $data);
$this->_customerSession = $customerSession;
}
public function getCustomerId()
{
if($this->_customerSession->isLoggedIn()):
return $this->_customerSession->getCustomer()->getId();
endif;
}
public function execute()
{
$this->_view->loadLayout();
$this->_view->getLayout()->initMessages();
$this->_view->renderLayout();
}
}
Now in phtml file
<?php
$customerId = $customer->getCustomerId();
?>
any suggestion or issue in code thankss
Update controller
<?php
namespace Vendor\Module\Controller\Index;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\View\Result\PageFactory;
class Customer 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();
$this->_view->loadLayout();
$this->_view->renderLayout();
}
}