createUrl() public method

You may specify the route as a string, e.g., site/index. You may also use an array if you want to specify additional query parameters for the URL being created. The array format must be: php generates: /index.php?r=site%2Findex¶m1=value1¶m2=value2 ['site/index', 'param1' => 'value1', 'param2' => 'value2'] If you want to create a URL with an anchor, you can use the array format with a # parameter. For example, php generates: /index.php?r=site%2Findex¶m1=value1#name ['site/index', 'param1' => 'value1', '#' => 'name'] The URL created is a relative one. Use UrlManager::createAbsoluteUrl to create an absolute URL. Note that unlike [[\yii\helpers\Url::toRoute()]], this method always treats the given route as an absolute route.
public createUrl ( string | array $params ) : string
$params string | array use a string to represent a route (e.g. `site/index`), or an array to represent a route with query parameters (e.g. `['site/index', 'param1' => 'value1']`).
return string the created URL
Esempio n. 1
21
 public function createUrl($params)
 {
     $url = parent::createUrl($params);
     if ($this->enableAppendLang) {
         $url = MultilangHelper::addLangToUrl($url);
     }
     return $url;
 }
Esempio n. 2
0
 public function createUrl($params)
 {
     if (isset($params['lang_id'])) {
         //Если указан идентификатор языка, то делаем попытку найти язык в БД,
         //иначе работаем с языком по умолчанию
         $lang = Lang::findOne($params['lang_id']);
         if ($lang === null) {
             $lang = Lang::getDefaultLang();
         }
         unset($params['lang_id']);
     } else {
         //Если не указан параметр языка, то работаем с текущим языком
         $lang = Lang::getCurrent();
     }
     //Получаем сформированный URL(без префикса идентификатора языка)
     $url = parent::createUrl($params);
     //Добавляем к URL префикс - буквенный идентификатор языка
     if ($url == '/') {
         return '/' . $lang->url;
     } else {
         if (YII_DEBUG && !empty(Yii::$app->params['mainPathCount'])) {
             $url_list = explode('/', $url);
             $mainPath = [];
             for ($i = 0; $i <= Yii::$app->params['mainPathCount']; $i++) {
                 $mainPath[] = array_shift($url_list);
             }
             $url_list = array_merge($mainPath, [$lang->url], $url_list);
             return join('/', $url_list);
         }
         return '/' . $lang->url . $url;
     }
 }
Esempio n. 3
0
 /**
  * [createUrl description]
  * @param  [type] $params [description]
  * @return [type]         [description]
  */
 public function createUrl($params)
 {
     $defaultLang = Lang::getDefaultLang();
     if (isset($params[1]) && isset($params[1]['lang_id'])) {
         // Если указан идентификатор языка, то делаем попытку найти язык в БД, иначе работаем с языком по умолчанию.
         $lang = Lang::findOne(['url' => $params[1]['lang_id']]);
         if ($lang === null) {
             $lang = $defaultLang;
         }
         unset($params[1]['lang_id']);
     } else {
         // Если не указан параметр языка, то работаем с текущим языком.
         $lang = Lang::getCurrent();
     }
     // Получаем сформированный URL(без префикса идентификатора языка).
     $url = parent::createUrl($params);
     if ($lang->url == $defaultLang->url) {
         return $url;
     }
     // Добавляем к URL префикс - буквенный идентификатор языка.
     if ($url == '/') {
         return '/' . $lang->url;
     } else {
         return '/' . $lang->url . $url;
     }
 }
Esempio n. 4
0
 /**
  * @inheritdoc
  */
 public function createUrl($params)
 {
     $params = (array) $params;
     $language = $this->getLanguage(isset($params['language']) ? $params['language'] : self::DEFAULT_KEY);
     unset($params['language']);
     return '/' . $language->url . '/' . ltrim(parent::createUrl($params), '/');
 }
Esempio n. 5
0
 /**
  * @inheritdoc
  */
 public function createUrl($params)
 {
     if (!isset($params['language'])) {
         $params['language'] = Yii::$app->language;
     }
     return parent::createUrl($params);
 }
Esempio n. 6
0
 /**
  * @inheritdoc
  */
 public function createUrl($params)
 {
     $params = (array) $params;
     $localeUrls = Yii::$app->localeUrls;
     if (isset($params[$this->languageParam])) {
         $language = $params[$this->languageParam];
         unset($params[$this->languageParam]);
         $languageRequired = true;
     } else {
         $language = Yii::$app->language;
         $languageRequired = false;
     }
     $url = parent::createUrl($params);
     // Unless a language was explicitely specified in the parameters we can return a URL without any prefix
     // for the default language, if suffixes are disabled for the default language. In any other case we
     // always add the suffix, e.g. to create "reset" URLs that explicitely contain the default language.
     if (!$languageRequired && !$localeUrls->enableDefaultSuffix && $language === $localeUrls->getDefaultLanguage()) {
         return $url;
     } else {
         $key = array_search($language, $localeUrls->languages);
         $base = $this->showScriptName ? $this->getScriptUrl() : $this->getBaseUrl();
         $length = strlen($base);
         if (is_string($key)) {
             $language = $key;
         }
         return $length ? substr_replace($url, "{$base}/{$language}", 0, $length) : "/{$language}{$url}";
     }
 }
 public function createUrl($params)
 {
     if ($this->selectedLanguageIsNotTheDefalutLanguage() && $this->isSelectedLanguageSupported()) {
         $params['language'] = Yii::$app->language;
     }
     return parent::createUrl($params);
 }
Esempio n. 8
0
 /**
  * Creates URL with language identifiers(domain and/or folder)
  * @param $params
  * @return string
  * @throws ServerErrorHttpException
  */
 private function createLanguageUrl($params)
 {
     /** @var \DevGroup\Multilingual\Multilingual $multilingual */
     $multilingual = Yii::$app->multilingual;
     $requested_language_id = isset($params[$this->languageParam]) ? $params[$this->languageParam] : null;
     if ($requested_language_id === null) {
         $requested_language_id = $multilingual->language_id;
     } else {
         unset($params[$this->languageParam]);
     }
     /** @var Language $requested_language */
     $requested_language = call_user_func([$multilingual->modelsMap['Language'], 'getById'], $requested_language_id);
     if ($requested_language === null) {
         throw new ServerErrorHttpException('Requested language not found');
     }
     $current_language_id = $multilingual->language_id;
     $url = parent::createUrl($params);
     if (!empty($requested_language->folder)) {
         $url = '/' . $requested_language->folder . '/' . ltrim($url, '/');
     }
     if ($current_language_id === $requested_language->id && $this->forceHostInUrl === false) {
         return $url;
     }
     if ($this->forceScheme !== null) {
         $scheme = $this->forceScheme;
     } else {
         $scheme = Yii::$app->request->getIsSecureConnection() ? 'https' : 'http';
     }
     if ($this->forcePort !== null) {
         $port = $this->forcePort === 80 ? '' : ':' . $this->forcePort;
     } else {
         $port = Yii::$app->request->port === 80 ? '' : ':' . Yii::$app->request->port;
     }
     return $scheme . '://' . $requested_language->domain . $port . '/' . ltrim($url, '/');
 }
 public function createUrl($params)
 {
     if (isset($params['language']) && $params['language'] === false || isset($params[0]) && in_array($params[0], $this->nonMultilingualUrls)) {
         unset($params['language']);
         return parent::createUrl($params);
     }
     if (Yii::$app->yee->isMultilingual && !empty($this->multilingualRules)) {
         $languages = array_keys(Yii::$app->yee->displayLanguages);
         //remove incorrect language param
         if (isset($params['language']) && !in_array($params['language'], $languages)) {
             unset($params['language']);
         }
         //trying to get language param
         if (!isset($params['language'])) {
             if (Yii::$app->session->has('language')) {
                 $language = Yii::$app->session->get('language');
             } elseif (isset(Yii::$app->request->cookies['language'])) {
                 $language = Yii::$app->request->cookies['language']->value;
             } else {
                 $language = Yii::$app->language;
             }
             if (in_array($language, $languages)) {
                 Yii::$app->language = $language;
             }
             $params['language'] = Yii::$app->yee->getDisplayLanguageShortcode(Yii::$app->language);
         }
     }
     return parent::createUrl($params);
 }
 public function createUrl($params)
 {
     if (isset($params['title'])) {
         $params['title'] = $this->clean(self::normal_chars($params['title']));
     }
     return parent::createUrl($params);
 }
Esempio n. 11
0
 public function createUrl($params)
 {
     if (isset($params['q'])) {
         unset($params['q']);
     }
     return parent::createUrl($params);
 }
Esempio n. 12
0
 /**
  * @param array|string $params
  * @param null|string $language языковой контекст обработки урла, позволяет определить для какого сайта(рускоязычного или допустим англоязычного)
  * нужно сделать урл, используется в MenuManager для определения соответсвующей карты меню
  * @return string
  */
 public function createUrl($params, $language = null)
 {
     $this->_language = isset($language) ? $language : ArrayHelper::getValue($params, static::LANGUAGE_PARAM, Yii::$app->language);
     if (is_array($params)) {
         unset($params[static::LANGUAGE_PARAM]);
     }
     return parent::createUrl($params);
 }
Esempio n. 13
0
 public function actionIndex()
 {
     \Yii::$app->language = 'ru-Ru';
     $u = new UrlManager();
     $newHome = $u->createUrl('items/index');
     //var_dump($st);exit();
     Yii::$app->homeUrl = $newHome;
     return $this->goHome();
 }
Esempio n. 14
0
 /**
  * Creates a URL using the given route and query parameters.
  * Add lang param before route
  */
 public function createUrl($params)
 {
     $params = array_merge(['lang' => $this->currLang], (array) $params);
     $lang = $params['lang'];
     if ($this->enablePrettyUrl) {
         unset($params['lang']);
     }
     $url = parent::createUrl($params);
     return $this->enablePrettyUrl ? '/' . $lang . ($url === '/' ? '' : $url) : $url;
 }
Esempio n. 15
0
 /**
  * @inheritdoc
  */
 public function createUrl($params)
 {
     $params = (array) $params;
     $anchor = isset($params['#']) ? $params['#'] : '';
     $localeUrls = Yii::$app->localeUrls;
     if (isset($params[$this->languageParam])) {
         $language = $params[$this->languageParam];
         unset($params[$this->languageParam]);
         $languageRequired = true;
     } else {
         $language = Yii::$app->language;
         $languageRequired = false;
     }
     // if disable param is set return default url
     if (isset($params[$this->disableLocalesParam])) {
         unset($params[$this->disableLocalesParam]);
         return parent::createUrl($params);
     }
     $url = parent::createUrl($params);
     // Unless a language was explicitely specified in the parameters we can return a URL without any prefix
     // for the default language, if suffixes are disabled for the default language. In any other case we
     // always add the suffix, e.g. to create "reset" URLs that explicitely contain the default language.
     if (!$languageRequired && !$localeUrls->enableDefaultSuffix && $language === $localeUrls->getDefaultLanguage()) {
         return $url;
     } else {
         $key = array_search($language, $localeUrls->languages);
         $base = $this->showScriptName ? $this->getScriptUrl() : $this->getBaseUrl();
         $length = strlen($base);
         if (is_string($key)) {
             $language = $key;
         }
         $parts = parse_url($url);
         if (isset($parts['scheme'], $parts['host'], $parts['path'])) {
             if ($length) {
                 $parts['path'] = trim(substr_replace($parts['path'], sprintf('%s/%s', $base, $language), 0, $length), '/');
             } else {
                 $parts['path'] = trim(sprintf('%s%s', $language, $parts['path']), '/');
             }
             $fullpath = sprintf('%s://%s/%s%s', $parts['scheme'], $parts['host'], $parts['path'], $this->suffix);
             if ($anchor) {
                 $fullpath = sprintf('%s#%s', $fullpath, $anchor);
             }
             if (isset($parts['query'])) {
                 return sprintf('%s?%s', $fullpath, $parts['query']);
             }
             return $fullpath;
         }
         if ($length) {
             $path = substr_replace($url, "{$base}/{$language}", 0, $length);
         } else {
             $path = "/{$language}{$url}";
         }
         return $path . $anchor;
     }
 }
Esempio n. 16
0
 /**
  * @param array|string $params
  * @return string
  */
 public function createUrl($params)
 {
     $params = (array) $params;
     $language = isset($params['language']) ? $params['language'] : Language::getCurrent();
     if (!Language::isLanguage($language)) {
         $language = Language::getCurrent();
     }
     unset($params['route'], $params['language']);
     $params['0'] = $language . '/' . trim($params['0'], '/');
     return parent::createUrl($params);
 }
Esempio n. 17
0
 public function createUrl($params = [])
 {
     //        if (!isset($params[$this->languageVar]) || !Lang::checkLang($params[$this->languageVar])) {
     //            $params[$this->languageVar] = Lang::get();
     //        }
     if ($this->_isExcluded($params[0])) {
         unset($params[$this->languageVar]);
     } else {
         $params[0] = '/' . Yii::$app->lang->get() . (ltrim($params[0], '/') ? '/' . ltrim($params[0], '/') : '');
     }
     return parent::createUrl($params);
 }
Esempio n. 18
0
 public function createUrl($params)
 {
     if (empty($params['lang'])) {
         if (Yii::$app->session->has('lang')) {
             Yii::$app->language = Yii::$app->session->get('lang', Yii::$app->language);
         } elseif (Yii::$app->request->cookies->getValue('lang')) {
             Yii::$app->language = Yii::$app->request->cookies->getValue('lang');
         }
         $params['lang'] = Yii::$app->language;
     }
     return parent::createUrl($params);
 }
 /**
  * createUrl
  * Adding language parameter to links
  *
  * @param array $params
  *
  * @return string
  */
 public function createUrl($params)
 {
     if (!isset($params['_language'])) {
         if (Yii::$app->session->has('_language')) {
             Yii::$app->language = Yii::$app->session->get('_language');
         } elseif (Yii::$app->request->cookies->has('_language')) {
             Yii::$app->language = Yii::$app->request->cookies->get('_language');
         }
         $params['_language'] = Yii::$app->language;
     }
     return parent::createUrl($params);
 }
 public function createUrl($params)
 {
     if (!isset($params["lang"])) {
         if (Yii::$app->session->has('lang')) {
             Yii::$app->language = Yii::$app->session->get('lang');
         } elseif (isset(Yii::$app->request->cookies["lang"])) {
             Yii::$app->language = Yii::$app->request->cookies["lang"]->value;
         } else {
             $params['lang'] = Yii::$app->language;
         }
     }
     return parent::createUrl($params);
 }
Esempio n. 21
0
 public function createUrl($params)
 {
     $langPrefix = '';
     if (Yii::$app->getModule('radiata')->activeLanguage->code != Yii::$app->getModule('radiata')->defaultLanguage->code) {
         $langPrefix = '/' . Yii::$app->getModule('radiata')->activeLanguage->code;
     }
     $url = parent::createUrl($params);
     $parsedUrl = parse_url($url);
     if (isset($parsedUrl['scheme']) && $parsedUrl['scheme'] != '') {
         return str_replace($parsedUrl['path'], $langPrefix . $parsedUrl['path'], $url);
     } else {
         return $langPrefix . $url;
     }
 }
Esempio n. 22
0
 /**
  * Creates translated url.
  * @param array $params
  * @return string the created URL
  */
 public function createUrl($params)
 {
     if (!empty($params[0])) {
         $route = explode('/', $params[0]);
         if (isset($route[0]) && !empty($this->languageControllers[$this->language][$route[0]])) {
             $route[0] = $this->languageControllers[$this->language][$route[0]];
         }
         if (isset($route[1]) && !empty($this->languageActions[$this->language][$route[1]])) {
             $route[1] = $this->languageActions[$this->language][$route[1]];
         }
         $params[0] = implode('/', $route);
     }
     return parent::createUrl($params);
 }
Esempio n. 23
0
 /** @inheritdoc */
 public function createUrl($params)
 {
     $params = (array) $params;
     if (array_key_exists($this->languageParam, $params)) {
         $language = Language::fromAlias($params[$this->languageParam]) ?: Language::getDefault();
     } else {
         $language = Language::fromLocal(\Yii::$app->language) ?: Language::getDefault();
     }
     $params[$this->languageParam] = $language->alias;
     if (!$this->showDefaultLanguage && $language->alias === Language::getDefault()->alias) {
         unset($params[$this->languageParam]);
     }
     return parent::createUrl($params);
 }
 public function createUrl($params)
 {
     // Url's language such as "en"
     $url_lang = Yii::$app->language;
     // If isset lang param
     if (isset($params['x-language-url'])) {
         // Do not change url
         if ($params['x-language-url'] === false) {
             unset($params['x-language-url']);
             return parent::createUrl($params);
         }
         // If exists language
         $xLang = LangHelper::getLanguageByParam('url', $params['x-language-url']);
         if ($xLang) {
             $url_lang = $xLang['url'];
         }
     }
     $current = LangHelper::getLanguageByParam('locale', $url_lang);
     $isDefault = isset($current['default']) && $current['default'];
     // Delete if is pretty url
     if ($this->enablePrettyUrl || $isDefault) {
         unset($params['x-language-url']);
     }
     $url = parent::createUrl($params);
     if ($current) {
         // Is curent language
         // Pretty url
         if ($this->enablePrettyUrl && !$this->showScriptName) {
             $pattern = "/^" . preg_quote($this->baseUrl, '/') . "/";
             $replaceTo = $this->baseUrl;
             if (!$isDefault) {
                 $replaceTo .= '/' . $url_lang;
             }
             $url = preg_replace($pattern, $replaceTo, $url);
         } else {
             if ($this->enablePrettyUrl && $this->showScriptName) {
                 // Pretty url with showScriptName
                 $pattern = "/^" . preg_quote($this->scriptUrl, '/') . "/";
                 $replaceTo = $this->baseUrl;
                 if (!$isDefault) {
                     $replaceTo .= '/' . $url_lang;
                 }
                 $url = preg_replace($pattern, $replaceTo, $url);
             }
         }
     }
     return $url;
 }
Esempio n. 25
0
 public function createUrl($params)
 {
     if (!Yii::$app->has('composition')) {
         return parent::createUrl($params);
     }
     $links = Yii::$app->get('links', false);
     $composition = Yii::$app->composition->getFull();
     $originalParams = $params;
     $params[0] = $composition . $params[0];
     $response = parent::createUrl($params);
     // we have not found the composition rule matching against rules, so use the original params and try again!
     if (strpos($response, $params[0]) !== false) {
         $params = $originalParams;
         $response = parent::createUrl($params);
     } else {
         // now we have to remove the composition informations from the links to make a valid link parsing (read module)
         $params[0] = str_replace($composition, '', $params[0]);
     }
     $params = (array) $params;
     $moduleName = \luya\helpers\Url::fromRoute($params[0], 'module');
     if ($this->getContextNavItemId() && $links !== null) {
         $link = $links->findOneByArguments(['id' => (int) $this->getContextNavItemId()]);
         $this->resetContext();
         return preg_replace("/{$moduleName}/", $composition . $link['url'], $response, 1);
     }
     $context = false;
     if ($moduleName !== false) {
         $moduleObject = \yii::$app->getModule($moduleName);
         if (method_exists($moduleObject, 'setContext') && !empty($moduleObject->context)) {
             if ($links !== null) {
                 $context = true;
                 $options = $moduleObject->getContextOptions();
                 $link = $links->findOneByArguments(['id' => (int) $options['navItemId']]);
                 $response = preg_replace("/{$moduleName}/", $composition . $link['url'], $response, 1);
             }
         }
     }
     if (!$context) {
         // because the urlCreation of yii returns a realtive url we have to manually add the composition getFull() path.
         $baseUrl = yii::$app->request->baseUrl;
         if (empty($baseUrl)) {
             $response = Url::removeTrailing($composition) . $response;
         } else {
             $response = str_replace($baseUrl, Url::trailing($baseUrl) . Url::removeTrailing($composition), $response);
         }
     }
     return $response;
 }
Esempio n. 26
0
 public function createUrl($params)
 {
     if (Yii::$app->params['multilingual'] == 0) {
         unset($params['lang']);
     }
     if (Yii::$app->params['multilingual'] && !isset($params['lang'])) {
         //            if (Yii::$app->request->cookies->has('lang')) {
         //                Yii::$app->language = Yii::$app->request->cookies->getValue('lang');
         //            } else if (Yii::$app->session->has('lang')) {
         //                Yii::$app->language = Yii::$app->session->get('lang');
         //            }
         $params[$this->languageParam] = Yii::$app->language;
     }
     return str_replace('%2F', '/', parent::createUrl($params));
     //return parent::createUrl($params);
 }
Esempio n. 27
0
 public function createUrl($params)
 {
     \Yii::info(\Yii::$app->language);
     $lang = Languages::getLanguageByUrl(\Yii::$app->language);
     // Get basic URL (without language prefix)
     $url = parent::createUrl($params);
     $baseUrl = $this->getBaseUrl();
     if ($baseUrl) {
         $url = substr($url, strlen($baseUrl));
     }
     // Add language prefix if not default
     if (!$lang) {
         return ($baseUrl ? $baseUrl : '') . '/' . $url;
     } else {
         return ($baseUrl ? $baseUrl : '') . "/{$lang->url}{$url}";
     }
 }
 public function createUrl($params)
 {
     if (isset($params['lang_id'])) {
         $lang = Language::findOne($params['lang_id']);
         if ($lang === null) {
             $lang = Language::getDefaultLang();
         }
         unset($params['lang_id']);
     } else {
         $lang = Language::getCurrent();
     }
     $url = parent::createUrl($params);
     if ($url == '/') {
         return '/' . $lang->url;
     } else {
         return '/' . $lang->url . $url;
     }
 }
Esempio n. 29
-1
 public function createUrl($params)
 {
     if (isset($params['lang_id'])) {
         //Если указан идентификатор языка, то делаем попытку найти язык в БД,
         //иначе работаем с языком по умолчанию
         $lang = Language::findOne($params['lang_id']);
         if ($lang === null) {
             $lang = \Yii::$app->languageManager->getDefault();
         }
         unset($params['lang_id']);
     } else {
         //Если не указан параметр языка, то работаем с текущим языком
         $lang = \Yii::$app->languageManager->getCurrent();
     }
     //Получаем сформированный URL(без префикса идентификатора языка)
     $url = parent::createUrl($params);
     if ($lang->is_default) {
         return $url;
     } else {
         if ($url == '/') {
             return '/' . $lang->url;
         } elseif (preg_match('/^https?/si', $url)) {
             return preg_replace('#(^https?:\\/\\/[a-zA-Z0-9\\.]+)#', '$1/' . $lang->url, $url);
         } else {
             return '/' . $lang->url . $url;
         }
     }
 }
 public function createUrl($params)
 {
     $default = Languages::all()->getConfigDefault();
     $current = Languages::all()->getConfigCurrent();
     $url_prefix = null;
     if (isset($params['x-language-url']) && !$params['x-language-url']) {
         $params['x-language-url'] = $default['url'];
     }
     if (isset($params['x-language-url']) && Languages::all()->getConfigByParam('url', $params['x-language-url'])) {
         $url_prefix = $params['x-language-url'] === $default['url'] && $this->multilanguageHideDefaultPrefix ? '' : $params['x-language-url'];
         unset($params['x-language-url']);
     }
     //        echo "url_prefix: ";
     //        var_export($url_prefix);
     //        echo "\n";
     //
     //        print_r($params);
     //        exit;
     if ($url_prefix === null) {
         // Иначе подставляем текущий язык
         $url_prefix = Yii::$app->request->getLanguageInurl();
     }
     $url = parent::createUrl($params);
     if ($url_prefix) {
         $url = '/' . $url_prefix . $url;
     }
     return $url;
 }