yii\web\UrlRule::createUrl PHP Method

createUrl() public method

Creates a URL according to the given route and parameters.
public createUrl ( UrlManager $manager, string $route, array $params ) : string | boolean
$manager UrlManager the URL manager
$route string the route. It should not have slashes at the beginning or the end.
$params array the parameters
return string | boolean the created URL, or `false` if this rule cannot be used for creating this URL.
    public function createUrl($manager, $route, $params)
    {
        if ($this->mode === self::PARSING_ONLY) {
            return false;
        }
        $tr = [];
        // match the route part first
        if ($route !== $this->route) {
            if ($this->_routeRule !== null && preg_match($this->_routeRule, $route, $matches)) {
                $matches = $this->substitutePlaceholderNames($matches);
                foreach ($this->_routeParams as $name => $token) {
                    if (isset($this->defaults[$name]) && strcmp($this->defaults[$name], $matches[$name]) === 0) {
                        $tr[$token] = '';
                    } else {
                        $tr[$token] = $matches[$name];
                    }
                }
            } else {
                return false;
            }
        }
        // match default params
        // if a default param is not in the route pattern, its value must also be matched
        foreach ($this->defaults as $name => $value) {
            if (isset($this->_routeParams[$name])) {
                continue;
            }
            if (!isset($params[$name])) {
                return false;
            } elseif (strcmp($params[$name], $value) === 0) {
                // strcmp will do string conversion automatically
                unset($params[$name]);
                if (isset($this->_paramRules[$name])) {
                    $tr["<{$name}>"] = '';
                }
            } elseif (!isset($this->_paramRules[$name])) {
                return false;
            }
        }
        // match params in the pattern
        foreach ($this->_paramRules as $name => $rule) {
            if (isset($params[$name]) && !is_array($params[$name]) && ($rule === '' || preg_match($rule, $params[$name]))) {
                $tr["<{$name}>"] = $this->encodeParams ? urlencode($params[$name]) : $params[$name];
                unset($params[$name]);
            } elseif (!isset($this->defaults[$name]) || isset($params[$name])) {
                return false;
            }
        }
        $url = trim(strtr($this->_template, $tr), '/');
        if ($this->host !== null) {
            $pos = strpos($url, '/', 8);
            if ($pos !== false) {
                $url = substr($url, 0, $pos) . preg_replace('#/+#', '/', substr($url, $pos));
            }
        } elseif (strpos($url, '//') !== false) {
            $url = preg_replace('#/+#', '/', $url);
        }
        if ($url !== '') {
            $url .= $this->suffix === null ? $manager->suffix : $this->suffix;
        }
        if (!empty($params) && ($query = http_build_query($params)) !== '') {
            $url .= '?' . $query;
        }
        return $url;
    }

Usage Example

コード例 #1
0
 /**
  * @param \yii\web\UrlManager $manager
  * @param string $route
  * @param array $params
  * @return bool|string
  */
 public function createUrl($manager, $route, $params)
 {
     if (!isset($params[$this->languageParam]) || !$params[$this->languageParam]) {
         $params[$this->languageParam] = I18N::getCurrentLanguageSlug();
     }
     return parent::createUrl($manager, $route, $params);
 }
All Usage Examples Of yii\web\UrlRule::createUrl