url

5 common.inc url($path = NULL, $query = NULL, $fragment = NULL, $absolute = FALSE)
6 common.inc url($path = NULL, $options = array())
7 common.inc url($path = NULL, array $options = array())
8 common.inc url($path = NULL, array $options = array())

Generate an internal Drupal URL.

Parameters

$path: The Drupal path being linked to, such as "admin/node".

$query: A query string to append to the link.

$fragment: A fragment identifier (named anchor) to append to the link.

$absolute: Whether to force the output to be an absolute link (beginning with http:). Useful for links that will be displayed outside the site, such as in an RSS feed.

Return value

an HTML string containing a link to the given path.

When creating links in modules, consider whether l() could be a better alternative than url().

Related topics

73 calls to url()

13 string references to 'url'

File

includes/common.inc, line 1462
Common functions that many Drupal modules will need to reference.

Code

function url($path = NULL, $query = NULL, $fragment = NULL, $absolute = FALSE) {
  global $base_url;

  static $script;

  if (empty($script)) {
    // On some web servers, such as IIS, we can't omit "index.php".  So, we
    // generate "index.php?q=foo" instead of "?q=foo" on anything that is not
    // Apache.
    $script = (strpos($_SERVER['SERVER_SOFTWARE'], 'Apache') === false) ? 'index.php' : '';
  }

  $path = drupal_get_path_alias($path);

  if (isset($fragment)) {
    $fragment = '#' . $fragment;
  }

  $base = ($absolute ? $base_url . '/' : '');

  if (variable_get('clean_url', '0') == '0') {
    if (isset($path)) {
      if (isset($query)) {
        return $base . $script . '?q=' . $path . '&' . $query . $fragment;
      }
      else {
        return $base . $script . '?q=' . $path . $fragment;
      }
    }
    else {
      if (isset($query)) {
        return $base . $script . '?' . $query . $fragment;
      }
      else {
        return $base . $fragment;
      }
    }
  }
  else {
    if (isset($path)) {
      if (isset($query)) {
        return $base . $path . '?' . $query . $fragment;
      }
      else {
        return $base . $path . $fragment;
      }
    }
    else {
      if (isset($query)) {
        return $base . $script . '?' . $query . $fragment;
      }
      else {
        return $base . $fragment;
      }
    }
  }
}
Login or register to post comments