8

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

9

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)));
1
  • 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
8

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.

7

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.

5

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

<?= $block->escapeUrl($block->getUrl('customer/account/login')); ?>
1
  • 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 that you have read and understand our privacy policy and code of conduct.

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