Browse Source

Support relative route in Controller::createUrl()

tags/2.0.0-beta
Qiang Xue 11 years ago
parent
commit
d1212bc73b
  1. 17
      framework/yii/web/Controller.php

17
framework/yii/web/Controller.php

@ -65,11 +65,15 @@ class Controller extends \yii\base\Controller
* Creates a URL using the given route and parameters. * Creates a URL using the given route and parameters.
* *
* This method enhances [[UrlManager::createUrl()]] by supporting relative routes. * This method enhances [[UrlManager::createUrl()]] by supporting relative routes.
* A relative route is a route without a slash, such as "view". If the route is an empty * A relative route is a route without a leading slash, such as "view", "post/view".
* string, [[route]] will be used; Otherwise, [[uniqueId]] will be prepended to a relative route.
* *
* After this route conversion, the method This method calls [[UrlManager::createUrl()]] * - If the route is an empty string, the current [[route]] will be used;
* to create a URL. * - 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 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 * @param array $params the parameters (name-value pairs) to be included in the generated URL
@ -78,8 +82,11 @@ class Controller extends \yii\base\Controller
public function createUrl($route, $params = array()) public function createUrl($route, $params = array())
{ {
if (strpos($route, '/') === false) { if (strpos($route, '/') === false) {
// a relative route // empty or an action ID
$route = $route === '' ? $this->getRoute() : $this->getUniqueId() . '/' . $route; $route = $route === '' ? $this->getRoute() : $this->getUniqueId() . '/' . $route;
} elseif ($route[0] !== '/') {
// relative to module
$route = ltrim($this->module->getUniqueId() . '/' . $route, '/');
} }
return Yii::$app->getUrlManager()->createUrl($route, $params); return Yii::$app->getUrlManager()->createUrl($route, $params);
} }

Loading…
Cancel
Save