|
|
|
@ -101,9 +101,9 @@ class Controller extends \yii\base\Controller
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Creates a URL using the given route and parameters. |
|
|
|
|
* Normalizes route making it suitable for UrlManager. Absolute routes are staying as is |
|
|
|
|
* while relative routes are converted to absolute routes. |
|
|
|
|
* |
|
|
|
|
* This method enhances [[UrlManager::createUrl()]] by supporting relative routes. |
|
|
|
|
* A relative route is a route without a leading slash, such as "view", "post/view". |
|
|
|
|
* |
|
|
|
|
* - If the route is an empty string, the current [[route]] will be used; |
|
|
|
@ -112,13 +112,10 @@ class Controller extends \yii\base\Controller
|
|
|
|
|
* - If the route has no leading slash, it is considered to be a route relative |
|
|
|
|
* to the current module and will be prepended with the module's uniqueId. |
|
|
|
|
* |
|
|
|
|
* After this route conversion, the method calls [[UrlManager::createUrl()]] to create a URL. |
|
|
|
|
* |
|
|
|
|
* @param string $route the route. This can be either an absolute route or a relative route. |
|
|
|
|
* @param array $params the parameters (name-value pairs) to be included in the generated URL |
|
|
|
|
* @return string the created URL |
|
|
|
|
* @return string normalized route suitable for UrlManager |
|
|
|
|
*/ |
|
|
|
|
public function createUrl($route, $params = []) |
|
|
|
|
protected function getNormalizedRoute($route) |
|
|
|
|
{ |
|
|
|
|
if (strpos($route, '/') === false) { |
|
|
|
|
// empty or an action ID |
|
|
|
@ -127,10 +124,58 @@ class Controller extends \yii\base\Controller
|
|
|
|
|
// relative to module |
|
|
|
|
$route = ltrim($this->module->getUniqueId() . '/' . $route, '/'); |
|
|
|
|
} |
|
|
|
|
return $route; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Creates a relative URL using the given route and parameters. |
|
|
|
|
* |
|
|
|
|
* This method enhances [[UrlManager::createUrl()]] by supporting relative routes. |
|
|
|
|
* A relative route is a route without a leading slash, such as "view", "post/view". |
|
|
|
|
* |
|
|
|
|
* - If the route is an empty string, the current [[route]] will be used; |
|
|
|
|
* - If the route contains no slashes at all, it is considered to be an action ID |
|
|
|
|
* of the current controller and will be prepended with [[uniqueId]]; |
|
|
|
|
* - If the route has no leading slash, it is considered to be a route relative |
|
|
|
|
* to the current module and will be prepended with the module's uniqueId. |
|
|
|
|
* |
|
|
|
|
* After this route conversion, the method calls [[UrlManager::createUrl()]] to create a URL. |
|
|
|
|
* |
|
|
|
|
* @param string $route the route. This can be either an absolute route or a relative route. |
|
|
|
|
* @param array $params the parameters (name-value pairs) to be included in the generated URL |
|
|
|
|
* @return string the created relative URL |
|
|
|
|
*/ |
|
|
|
|
public function createUrl($route, $params = []) |
|
|
|
|
{ |
|
|
|
|
$route = $this->getNormalizedRoute($route); |
|
|
|
|
return Yii::$app->getUrlManager()->createUrl($route, $params); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Creates an absolute URL using the given route and parameters. |
|
|
|
|
* |
|
|
|
|
* This method enhances [[UrlManager::createAbsoluteUrl()]] by supporting relative routes. |
|
|
|
|
* A relative route is a route without a leading slash, such as "view", "post/view". |
|
|
|
|
* |
|
|
|
|
* - If the route is an empty string, the current [[route]] will be used; |
|
|
|
|
* - If the route contains no slashes at all, it is considered to be an action ID |
|
|
|
|
* of the current controller and will be prepended with [[uniqueId]]; |
|
|
|
|
* - If the route has no leading slash, it is considered to be a route relative |
|
|
|
|
* to the current module and will be prepended with the module's uniqueId. |
|
|
|
|
* |
|
|
|
|
* After this route conversion, the method calls [[UrlManager::createUrl()]] to create a URL. |
|
|
|
|
* |
|
|
|
|
* @param string $route the route. This can be either an absolute route or a relative route. |
|
|
|
|
* @param array $params the parameters (name-value pairs) to be included in the generated URL |
|
|
|
|
* @return string the created absolute URL |
|
|
|
|
*/ |
|
|
|
|
public function createAbsoluteUrl($route, $params = []) |
|
|
|
|
{ |
|
|
|
|
$route = $this->getNormalizedRoute($route); |
|
|
|
|
return Yii::$app->getUrlManager()->createAbsoluteUrl($route, $params); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Returns the canonical URL of the currently requested page. |
|
|
|
|
* The canonical URL is constructed using [[route]] and [[actionParams]]. You may use the following code |
|
|
|
|
* in the layout view to add a link tag about canonical URL: |
|
|
|
|