16

In Magento if I have a URL string, is there any built in function that I can use to add query parameters to it?

In the exact case I am looking at I am generating a category URL and then I want to add a set of query parameters on top of it.

I am doing the following:

$categoryUrl = Mage::getModel('catalog/category')->load(1)->getUrl();

I want to then add the following query parameters:

array('param1' => 'myparam1','param2' => 'myparam2');

I then want to end up with:

http://mysite.com/mycategory.html?param1=myparam1&param2=myparam2
2
  • Can you give an example of what you are trying to obtain? How are you generating the category url and what is the desired result?
    – Marius
    Jun 21, 2013 at 11:36
  • Updated question to reflect this Jun 21, 2013 at 11:43

3 Answers 3

10

I don't think there is a 'built in' way to achieve this.
The method Mage_Catalog_Model_Url::getUrl() does not accept any parameters. You can override the method, but unless you need something like this for all categories I don't see a point on doing it.
I think the cheapest way is to add the parameters directly.

$params = array('param1' => 'myparam1','param2' => 'myparam2');
$categoryUrl = Mage::getModel('catalog/category')->load(1)->getUrl();
$urlParams = array();
foreach ($params as $name=>$value){
    $urlParams[] = $name.'='.urlencode($value);
}
$urlParams = implode('&', $urlParams);
if ($urlParams){
    $glue = '?';
    if (strpos($categoryUrl, $glue) !== false){//this should never happen - but just in case
        $glue = '&';
    }
    $categoryUrl .= $glue.$urlParams;
}

You can make this a method in a helper to avoid writing the code each time you need it.

For any other url that you construct through Mage::getUrl() you can pass query params like this:

$url = Mage::getUrl('module/controller/action', array('param1'=>'val1', '_query'=>array('p1'=>'v1', 'p2'=>'v2')));

the code above will generate the following:

http://mysite.com/module/controller/action/param1/val1/?p1=v1&p2=v2

6
  • @Simon...when downvoting, give a reason.
    – Marius
    Jul 6, 2014 at 19:43
  • sorry, was a bit too fast - did not like the first code block, because there is a built-in way to achieve this, but you also show that way, so all good.
    – Simon
    Jul 9, 2014 at 6:52
  • ... and now I cannot change my vote any more (which I dislike a lot at all SE sites). Anyway, I think it is quite strange that you can see who downvoted your answer :/
    – Simon
    Jul 9, 2014 at 6:53
  • @Simon. I think I was a bit wrong on this one. See what benmarks wrote in an other answer. So your down vote had a good reason. I just stated that you should give a reason when downvoting. And rest assured, you cannot see who downvoted answers on SO. I just investigated a bit. Because this website does not have a huge amount of votes and users it's kind of easy to find out who downvoted. I will not tell you how I found out, but all I can say is that it's kind of easy to see who downvoted on Sundays and Mondays. :).
    – Marius
    Jul 9, 2014 at 6:56
  • LOL okay :) Yeah, I think Bens answer should have been the accepted one on this question. Anyway, thanks and keep up the good work!
    – Simon
    Jul 9, 2014 at 7:00
27
echo Mage::getUrl('',
    array(
        '_direct' => Mage::getModel('core/url_rewrite')->loadByIdPath('category/1')->getRequestPath(),
        '_query' => array('param1' => 'myparam1','param2' => 'myparam2')
    )
);
3
  • 2
    This is the real answer. Nov 1, 2015 at 19:30
  • I don't think that there is a best answer to this, as passing params in a URL is not specific to Magento, although Magento contains a method for it. Jan 11, 2016 at 14:09
  • 1
    @TylerMaginnis "is there any built in function [in Magento] that I can use to add query parameters [to a URL string]?"
    – benmarks
    Jan 11, 2016 at 16:56
8

You can use core/url helper:

$params = array('param1' => 'myparam1', 'param2' => 'myparam2');
$newCategoryUrl = Mage::helper('core/url')->addRequestParam($categoryUrl, $params);

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.