Browse Source

url wip

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
0093d53432
  1. 90
      framework/web/UrlManager.php

90
framework/web/UrlManager.php

@ -67,28 +67,33 @@ class UrlManager extends Component
parent::init(); parent::init();
if ($this->enablePrettyUrl && $this->rules !== array()) { if ($this->enablePrettyUrl && $this->rules !== array()) {
/** $this->compileRules();
* @var $cache \yii\caching\Cache }
*/ }
if ($this->cacheID !== false && ($cache = Yii::$app->getComponent($this->cacheID)) !== null) {
$key = $cache->buildKey(__CLASS__);
$hash = md5(json_encode($this->rules));
if (($data = $cache->get($key)) !== false && isset($data[1]) && $data[1] === $hash) {
$this->rules = $data[0];
return;
}
}
foreach ($this->rules as $i => $rule) { protected function compileRules()
if (!isset($rule['class'])) { {
$rule['class'] = $this->defaultRuleClass; /**
} * @var $cache \yii\caching\Cache
$this->rules[$i] = Yii::createObject($rule); */
if ($this->cacheID !== false && ($cache = Yii::$app->getComponent($this->cacheID)) !== null) {
$key = $cache->buildKey(__CLASS__);
$hash = md5(json_encode($this->rules));
if (($data = $cache->get($key)) !== false && isset($data[1]) && $data[1] === $hash) {
$this->rules = $data[0];
return;
} }
}
if (isset($cache)) { foreach ($this->rules as $i => $rule) {
$cache->set($key, array($this->rules, $hash)); if (!isset($rule['class'])) {
$rule['class'] = $this->defaultRuleClass;
} }
$this->rules[$i] = Yii::createObject($rule);
}
if (isset($cache)) {
$cache->set($key, array($this->rules, $hash));
} }
} }
@ -98,7 +103,7 @@ class UrlManager extends Component
* @return array|boolean the route and the associated parameters. The latter is always empty * @return array|boolean the route and the associated parameters. The latter is always empty
* if [[enablePrettyUrl]] is false. False is returned if the current request cannot be successfully parsed. * if [[enablePrettyUrl]] is false. False is returned if the current request cannot be successfully parsed.
*/ */
public function parseUrl($request) public function parseRequest($request)
{ {
if ($this->enablePrettyUrl) { if ($this->enablePrettyUrl) {
$pathInfo = $request->pathInfo; $pathInfo = $request->pathInfo;
@ -135,11 +140,13 @@ class UrlManager extends Component
$route = trim($route, '/'); $route = trim($route, '/');
$baseUrl = $this->getBaseUrl();
if ($this->enablePrettyUrl) { if ($this->enablePrettyUrl) {
/** @var $rule UrlRule */ /** @var $rule UrlRule */
foreach ($this->rules as $rule) { foreach ($this->rules as $rule) {
if (($url = $rule->createUrl($this, $route, $params)) !== false) { if (($url = $rule->createUrl($this, $route, $params)) !== false) {
return $this->getBaseUrl() . $url . $anchor; return $baseUrl . $url . $anchor;
} }
} }
@ -149,17 +156,39 @@ class UrlManager extends Component
if ($params !== array()) { if ($params !== array()) {
$route .= '?' . http_build_query($params); $route .= '?' . http_build_query($params);
} }
return $this->getBaseUrl() . '/' . $route . $anchor; return $baseUrl . '/' . $route . $anchor;
} else { } else {
$params[$this->routeVar] = $route; $params[$this->routeVar] = $route;
return $this->getBaseUrl() . '?' . http_build_query($params) . $anchor; if (!$this->showScriptName) {
$baseUrl .= '/';
}
return $baseUrl . '?' . http_build_query($params) . $anchor;
} }
} }
public function createAbsoluteUrl($route, $params = array(), $hostInfo = null)
{
if ($hostInfo === null) {
$hostInfo = $this->getHostInfo();
}
return $hostInfo . $this->createUrl($route, $params);
}
private $_baseUrl; private $_baseUrl;
/**
* Returns the base URL of the application.
* @return string the base URL of the application (the part after host name and before query string).
* If {@link showScriptName} is true, it will include the script name part.
* Otherwise, it will not, and the ending slashes are stripped off.
*/
public function getBaseUrl() public function getBaseUrl()
{ {
if ($this->_baseUrl === null) {
/** @var $request \yii\web\Request */
$request = Yii::$app->getRequest();
$this->_baseUrl = $this->showScriptName ? $request->getScriptUrl() : $request->getBaseUrl();
}
return $this->_baseUrl; return $this->_baseUrl;
} }
@ -168,6 +197,23 @@ class UrlManager extends Component
$this->_baseUrl = trim($value, '/'); $this->_baseUrl = trim($value, '/');
} }
private $_hostInfo;
public function getHostInfo()
{
if ($this->_hostInfo === null) {
/** @var $request \yii\web\Request */
$request = Yii::$app->getRequest();
$this->_hostInfo = $request->getHostInfo();
}
return $this->_baseUrl;
}
public function setHostInfo($value)
{
$this->_hostInfo = $value;
}
/** /**
* Removes the URL suffix from path info. * Removes the URL suffix from path info.
* @param string $pathInfo path info part in the URL * @param string $pathInfo path info part in the URL

Loading…
Cancel
Save