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

4 Answers 4


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 I need this login button in a phtml file with the referrer link like this solution, then what will be the $this and $block, could you plz tell. Sep 14, 2021 at 10:54

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.


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

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

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.