Get login URL on product page Magento 2

You could do a direct call:

$this->getUrl('customer/account/login');

Please notice, when using this instead of the variant above, the customer will not be redirected to the previous page after logging in.


Extend Product's block in your custom module's block file.

In your block's constructor inject object of \Magento\Customer\Model\Url. It should be something like

public function __construct(
    // some other code 
    \Magento\Customer\Model\Url $customerUrl,
    // snip
) {
    $this->_customerUrl = $customerUrl;
    // some other code
}

and function

public function getCustomerLoginUrl() {   
    return $this->_customerUrl->getLoginUrl();
}

Now You can call getCustomerLoginUrl() function to get Login URL.

Check Magento\Customer\Block\Form\Register class for details.


Thanks for your help everyone, this was the solution to get back to the referral page when logging in:

$url  = $this->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true]);
$login_url = $block->getUrl('customer/account/login', array('referer' => base64_encode($url)));