How to get a url parameter in Magento controller?
Solution 1
Magento's default routing algorithm uses three part URLs.
http://example.com/front-name/controller-name/action-method
So when you call
http://example.com/path/action/id/123
The word path
is your front name, action
is your controller name, and id
is your action method. After these three methods, you can use getParam
to grab a key/value pair
http://example.com/path/action/id/foo/123
//in a controller
var_dump($this->getRequest()->getParam('foo'));
You may also use the getParams
method to grab an array of parameters
$this->getRequest()->getParams()
Solution 2
If your url is the following structure: http://yoursiteurl.com/index.php/admin/sales_order_invoice/save/order_id/1795/key/b62f67bcaa908cdf54f0d4260d4fa847/
then use:
echo $this->getRequest()->getParam('order_id'); // output is 1795
If you want to get All Url Value or Parameter value than use below code.
var_dump($this->getRequest()->getParams());
If your url is like this: http://magentoo.blogspot.com/magentooo/userId=21
then use this to get the value of url
echo $_GET['userId'];
If you want more info about this click here.
Related videos on Youtube
jogi99
Updated on July 05, 2022Comments
-
jogi99 almost 2 years
Is there a Magento function to get the value of "id" from this url:
http://example.com/path/action/id/123
I know I can split the url on "/" to get the value, but I'd prefer a single function.
This doesn't work:
$id = $this->getRequest()->getParam('id');
It only works if I use http://example.com/path/action?id=123
-
Josua Marcel C over 10 years$id = $this->getRequest()->getParam('id'); this is only work in class that extends to Mage_Adminhtml_Controller_Action (on admin) or Mage_Core_Controller_Front_Action (on frontend). if that didn't work, it means your controller's not called.
-
-
jogi99 over 10 yearsI see. However I think the problem was also because I hadn't defined a router. I was using a rewrite:
-
jogi99 over 10 years<config> <global> <rewrite> <whatever> <from><![CDATA[#^/?ajax/product/item#]]></from> <to>/mymod/mycon</to> </whatever> </global> </config>