Skip to content

How to get Logged in Customer ID

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 VendorModuleBlock;
use MagentoFrameworkViewElementTemplate;
use MagentoBackendBlockTemplateContext;

class Rfq extends MagentoFrameworkViewElementTemplate
{    
    protected $_customerSession;    
    public function __construct(Context $context,
        MagentoCustomerModelSession $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 VendorModuleControllerIndex;
use MagentoFrameworkAppActionAction;
use MagentoFrameworkAppActionContext;
use MagentoFrameworkAppRequestInterface;
use MagentoFrameworkViewResultPageFactory;

class Customer extends MagentoFrameworkAppActionAction
{
    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(); 
    }


  
}