public function createUrl($params)
{
$params = (array) $params;
$anchor = isset($params['#']) ? '#' . $params['#'] : '';
unset($params['#'], $params[$this->routeParam]);
$route = trim($params[0], '/');
unset($params[0]);
$baseUrl = $this->showScriptName || !$this->enablePrettyUrl ? $this->getScriptUrl() : $this->getBaseUrl();
if ($this->enablePrettyUrl) {
$cacheKey = $route . '?';
foreach ($params as $key => $value) {
if ($value !== null) {
$cacheKey .= $key . '&';
}
}
$url = $this->getUrlFromCache($cacheKey, $route, $params);
if ($url === false) {
$cacheable = true;
foreach ($this->rules as $rule) {
/* @var $rule UrlRule */
if (!empty($rule->defaults) && $rule->mode !== UrlRule::PARSING_ONLY) {
// if there is a rule with default values involved, the matching result may not be cached
$cacheable = false;
}
if (($url = $rule->createUrl($this, $route, $params)) !== false) {
if ($cacheable) {
$this->setRuleToCache($cacheKey, $rule);
}
break;
}
}
}
if ($url !== false) {
if (strpos($url, '://') !== false) {
if ($baseUrl !== '' && ($pos = strpos($url, '/', 8)) !== false) {
return substr($url, 0, $pos) . $baseUrl . substr($url, $pos) . $anchor;
} else {
return $url . $baseUrl . $anchor;
}
} else {
return "{$baseUrl}/{$url}{$anchor}";
}
}
if ($this->suffix !== null) {
$route .= $this->suffix;
}
if (!empty($params) && ($query = http_build_query($params)) !== '') {
$route .= '?' . $query;
}
return "{$baseUrl}/{$route}{$anchor}";
} else {
$url = "{$baseUrl}?{$this->routeParam}=" . urlencode($route);
if (!empty($params) && ($query = http_build_query($params)) !== '') {
$url .= '&' . $query;
}
return $url . $anchor;
}
}