Browse Source

url wip

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

56
framework/web/UrlManager.php

@ -67,6 +67,12 @@ class UrlManager extends Component
parent::init();
if ($this->enablePrettyUrl && $this->rules !== array()) {
$this->compileRules();
}
}
protected function compileRules()
{
/**
* @var $cache \yii\caching\Cache
*/
@ -90,7 +96,6 @@ class UrlManager extends Component
$cache->set($key, array($this->rules, $hash));
}
}
}
/**
* Parses the user request.
@ -98,7 +103,7 @@ class UrlManager extends Component
* @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.
*/
public function parseUrl($request)
public function parseRequest($request)
{
if ($this->enablePrettyUrl) {
$pathInfo = $request->pathInfo;
@ -135,11 +140,13 @@ class UrlManager extends Component
$route = trim($route, '/');
$baseUrl = $this->getBaseUrl();
if ($this->enablePrettyUrl) {
/** @var $rule UrlRule */
foreach ($this->rules as $rule) {
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()) {
$route .= '?' . http_build_query($params);
}
return $this->getBaseUrl() . '/' . $route . $anchor;
return $baseUrl . '/' . $route . $anchor;
} else {
$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;
/**
* 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()
{
if ($this->_baseUrl === null) {
/** @var $request \yii\web\Request */
$request = Yii::$app->getRequest();
$this->_baseUrl = $this->showScriptName ? $request->getScriptUrl() : $request->getBaseUrl();
}
return $this->_baseUrl;
}
@ -168,6 +197,23 @@ class UrlManager extends Component
$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.
* @param string $pathInfo path info part in the URL

Loading…
Cancel
Save