public function UrlGenerator::httpBuildQuery

Parses an array into a valid, rawurlencoded query string.

This differs from http_build_query() as we need to rawurlencode() (instead of urlencode()) all query parameters.

Parameters

$query: The query parameter array to be processed, e.g. $_GET.

$parent: Internal use only. Used to build the $query array key for nested items.

Return value

A rawurlencoded string which can be used as or appended to the URL query string.

See also

drupal_get_query_parameters()

Related topics

1 call to UrlGenerator::httpBuildQuery()

File

drupal/core/lib/Drupal/Core/Routing/UrlGenerator.php, line 347
Contains Drupal\Core\Routing\UrlGenerator.

Class

UrlGenerator
A Generator creates URL strings based on a specified route.

Namespace

Drupal\Core\Routing

Code

public function httpBuildQuery(array $query, $parent = '') {
  $params = array();
  foreach ($query as $key => $value) {
    $key = $parent ? $parent . '[' . rawurlencode($key) . ']' : rawurlencode($key);

    // Recurse into children.
    if (is_array($value)) {
      $params[] = $this
        ->httpBuildQuery($value, $key);
    }
    elseif (!isset($value)) {
      $params[] = $key;
    }
    else {

      // For better readability of paths in query strings, we decode slashes.
      $params[] = $key . '=' . str_replace('%2F', '/', rawurlencode($value));
    }
  }
  return implode('&', $params);
}