I would like to get the getLoginUrl()
url to put a button to login to my product detail view. Any idea how?
4 Answers
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:
$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.
If you are talking about the phtml layer you could use:
<?= $block->escapeUrl($block->getUrl('customer/account/login')); ?>