Magento get current url with and without parameters
You can get the current page URL and it’s parameters (if any) by using getCurrentUrl() method in Magento. Below code will show you how to use it. Consider for example you have this url:
http://loca.lho.st/review/product/list/id/27/name/sony
To get this (current) URL in your module:
$currentUrl = $this->helper('core/url')->getCurrentUrl(); | |
//Gives: http://loca.lho.st/review/product/list/id/27/name/sony |
To get current URL parameters:
$params = $this->getRequest()->getParams(); //all the parameters | |
//Gives: Array ( [id] => 27 [name] => sony ) | |
$param = $this->getRequest()->getParam('name'); //parameter "name" | |
//Gives: sony |
To get only URL without parameters:
$request = $this->getRequest(); | |
$urlWithoutParameters = $this->getBaseUrl() . $request->getRouteName() .DS. $request->getControllerName() .DS. $request->getActionName(); | |
//Gives: http://loca.lho.st/review/product/list |
3 Comments
Leave a comment to kalpesh
Welcome to my Blog
Certifications
Honor
Recognition
Contributions
Categories
- Apache (2)
- ChatGPT (1)
- Domain name (2)
- eCommerce (2)
- htaccess (1)
- Humor (3)
- Instagram API (1)
- jQuery (4)
- JSON (1)
- Linux (10)
- Magento (142)
- Magento admin (58)
- Magento Certification (5)
- Magento error (13)
- Magento frontend (68)
- Magento Imagine (2)
- Magento Interview (5)
- Magento Master (2)
- Magento2 (10)
- Mobile (1)
- MySQL (7)
- OpenAI (1)
- OroCRM (2)
- Performance (2)
- PHP (8)
- Prototype JS (3)
- Security (4)
- Wordpress (3)
- XML (2)
Tag Cloud
500 internal server error admin answers attribute bug category checkbox checkout cookie customer difference domain name EAV error event extension interview invoice jquery linux magento magento2 magento admin magento error magento interview questions magento orm mysql observer order pinterest product products questions redirect register remove script session simplexml to array state status study guide tax url wordpress
$currentUrl = Mage::helper(‘core/url’)->getCurrentUrl()
or
$currentUrl = Mage::getUrl(‘*/*/*’, array(‘_current’ => true));
above code may not work always as expected. Better way to find the current url is to use the following code:
if (!in_array(Mage::app()->getFrontController()->getAction()->getFullActionName(), array(‘cms_index_noRoute’, ‘cms_index_defaultNoRoute’))) {
$currentUrl = Mage::helper(‘core/url’)->getCurrentUrl();
}
Source: http://www.blog.magepsycho.com/how-to-find-current-url-in-magento/
Hi Raj. Your website shows error as Mysql is down on your server.
Hi Kalpesh,
I had a question regarding passing a variable to the Forgot password page. From the Login page, we have a link which takes you to the forgot password page. The user might enter his email address in the Login page and then click on Forgot password link and go to the new page. What I am trying to do is get the email address that was entered on the login page to be displayed on the Forgot password page. I tried checking incase it is coming by the Post method, but that did not work.
getRequest()->getPost();
if (isset($postData[‘login[username]’])) {
echo $postData[‘login[username]’];
}
?>
The URL looks like: http://127.0.0.1/bcs/index.php/customer/account/forgotpassword/
So nothing in the GET method.
Any idea how I could retrieve the email address?
Thanks,
Neet