I would like to get the getLoginUrl() url to put a button to login to my product detail view. Any idea how?


You could do a direct call:


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)));

If you are talking about the phtml layer you could use:

<?= $block->escapeUrl($block->getUrl('customer/account/login')); ?>
  • how to get the referral URL? – jafar pinjar Oct 25 '18 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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