1

Problem

I'm trying to come up with a consistent way to get the current URL of the page, including parameters from both the key/value/key/value structure and the ?key=value&key=value structure.

What I've tried

The simplest method that I've found works like this:

$params = $this->getRequest()->getParams();
$params = array_merge($params, $newParams);
$url = $this->getUrl('*/*/*', $params);

However, this doesn't escape parameter values at all. So I do that myself like this:

$params = $this->getRequest()->getParams();
$params = array_merge($params, $newParams);

foreach ($params as $k => &$v) {
    $v= urlencode($v);
}

$url = $this->getUrl('*/*/*', $params);

Okay great, now I have encoded parameters. But what if the parameters include the / character? You end up with a URL like this: my_module/my_controller/my_action/FromEntryDate/12%2F06%2F2014/ToEntryDate/06%2F04%2F2015/

But if you do Zend_Debug::dump($this->getRequest()); you get this:

array(5) {
  ["FromEntryDate"] => string(2) "12"
  ["06"] => string(4) "2014"
  ["ToEntryDate"] => string(2) "06"
  ["04"] => string(4) "2015"
}

So clearly the / aren't being interpreted correctly, even though they are encoded. So I end up with this work around instead:

$r = $this->getRequest();

$params = $r->getParams();
$params = array_merge($params, $newParams);

$url = implode('/', array(
    $r->getBaseUrl(),
    $r->getRouteName(),
    $r->getControllerName(),
    $r->getActionName()
)).'?'.http_build_query($params);

Questions

  1. Is there no way to encode / characters so that Magento will parse the param value correctly?

  2. Is there not a built in function to do this already!?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.