UrlManager.php 6.22 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8 9 10 11
<?php
/**
 * UrlManager class file
 *
 * @link http://www.yiiframework.com/
 * @copyright Copyright &copy; 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\web;

Qiang Xue committed
12
use Yii;
Qiang Xue committed
13 14 15 16 17 18 19 20 21 22
use \yii\base\Component;

/**
 * UrlManager manages the URLs of Yii applications.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class UrlManager extends Component
{
Qiang Xue committed
23
	/**
Qiang Xue committed
24 25 26 27 28 29 30 31 32
	 * @var boolean whether to enable pretty URLs.
	 */
	public $enablePrettyUrl = false;
	/**
	 * @var array the rules for creating and parsing URLs when [[enablePrettyUrl]] is true.
	 * This property is used only if [[enablePrettyUrl]] is true. Each element in the array
	 * is the configuration of creating a single URL rule whose class by default is [[defaultRuleClass]].
	 * If you modify this property after the UrlManager object is created, make sure
	 * populate the array with the rule objects instead of configurations.
Qiang Xue committed
33 34 35 36
	 */
	public $rules = array();
	/**
	 * @var string the URL suffix used when in 'path' format.
Qiang Xue committed
37 38
	 * For example, ".html" can be used so that the URL looks like pointing to a static HTML page.
	 * This property is used only if [[enablePrettyUrl]] is true.
Qiang Xue committed
39
	 */
Qiang Xue committed
40
	public $suffix;
Qiang Xue committed
41 42
	/**
	 * @var boolean whether to show entry script name in the constructed URL. Defaults to true.
Qiang Xue committed
43
	 * This property is used only if [[enablePrettyUrl]] is true.
Qiang Xue committed
44 45 46 47 48
	 */
	public $showScriptName = true;
	/**
	 * @var string the GET variable name for route. Defaults to 'r'.
	 */
Qiang Xue committed
49
	public $routeVar = 'r';
Qiang Xue committed
50
	/**
Qiang Xue committed
51 52
	 * @var string the ID of the cache component that is used to cache the parsed URL rules.
	 * Defaults to 'cache' which refers to the primary cache component registered with the application.
Qiang Xue committed
53 54 55 56
	 * Set this property to false if you want to disable caching URL rules.
	 */
	public $cacheID = 'cache';
	/**
Qiang Xue committed
57 58
	 * @var string the class name or configuration for URL rule instances.
	 * This will be passed to [[\Yii::createObject()]] to create the URL rule instances.
Qiang Xue committed
59
	 */
Qiang Xue committed
60
	public $defaultRuleClass = 'yii\web\UrlRule';
Qiang Xue committed
61 62 63 64 65 66 67 68

	/**
	 * Initializes the application component.
	 */
	public function init()
	{
		parent::init();

Qiang Xue committed
69
		if ($this->enablePrettyUrl && $this->rules !== array()) {
Qiang Xue committed
70 71 72
			$this->compileRules();
		}
	}
Qiang Xue committed
73

Qiang Xue committed
74 75 76 77 78 79 80 81 82 83 84
	protected function 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;
Qiang Xue committed
85
			}
Qiang Xue committed
86
		}
Qiang Xue committed
87

Qiang Xue committed
88 89 90
		foreach ($this->rules as $i => $rule) {
			if (!isset($rule['class'])) {
				$rule['class'] = $this->defaultRuleClass;
Qiang Xue committed
91
			}
Qiang Xue committed
92 93 94 95 96
			$this->rules[$i] = Yii::createObject($rule);
		}

		if (isset($cache)) {
			$cache->set($key, array($this->rules, $hash));
Qiang Xue committed
97
		}
Qiang Xue committed
98 99 100 101
	}

	/**
	 * Parses the user request.
Qiang Xue committed
102 103 104
	 * @param Request $request the request 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.
Qiang Xue committed
105
	 */
Qiang Xue committed
106
	public function parseRequest($request)
Qiang Xue committed
107
	{
Qiang Xue committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
		if ($this->enablePrettyUrl) {
			$pathInfo = $request->pathInfo;
			/** @var $rule UrlRule */
			foreach ($this->rules as $rule) {
				if (($result = $rule->parseUrl($this, $pathInfo)) !== false) {
					return $result;
				}
			}

			$suffix = (string)$this->suffix;
			if ($suffix !== '' && $suffix !== '/' && $pathInfo !== '') {
				$n = strlen($this->suffix);
				if (substr($pathInfo, -$n) === $this->suffix) {
					$pathInfo = substr($pathInfo, 0, -$n);
					if ($pathInfo === '') {
						// suffix alone is not allowed
						return false;
					}
				}
			}

			return array($pathInfo, array());
		} else {
			$route = (string)$request->getParam($this->routeVar);
			return array($route, array());
		}
Qiang Xue committed
134 135 136 137 138 139 140
	}

	public function createUrl($route, $params = array())
	{
		$anchor = isset($params['#']) ? '#' . $params['#'] : '';
		unset($anchor['#']);

Qiang Xue committed
141 142
		$route = trim($route, '/');

Qiang Xue committed
143 144
		$baseUrl = $this->getBaseUrl();

Qiang Xue committed
145 146 147 148
		if ($this->enablePrettyUrl) {
			/** @var $rule UrlRule */
			foreach ($this->rules as $rule) {
				if (($url = $rule->createUrl($this, $route, $params)) !== false) {
Qiang Xue committed
149
					return $baseUrl . $url . $anchor;
Qiang Xue committed
150
				}
Qiang Xue committed
151 152
			}

Qiang Xue committed
153 154 155 156 157 158
			if ($this->suffix !== null) {
				$route .= $this->suffix;
			}
			if ($params !== array()) {
				$route .= '?' . http_build_query($params);
			}
Qiang Xue committed
159
			return $baseUrl . '/' . $route . $anchor;
Qiang Xue committed
160 161
		} else {
			$params[$this->routeVar] = $route;
Qiang Xue committed
162 163 164 165
			if (!$this->showScriptName) {
				$baseUrl .= '/';
			}
			return $baseUrl . '?' . http_build_query($params) . $anchor;
Qiang Xue committed
166
		}
Qiang Xue committed
167
	}
Qiang Xue committed
168

Qiang Xue committed
169 170 171 172 173 174 175 176
	public function createAbsoluteUrl($route, $params = array(), $hostInfo = null)
	{
		if ($hostInfo === null) {
			$hostInfo = $this->getHostInfo();
		}
		return $hostInfo . $this->createUrl($route, $params);
	}

Qiang Xue committed
177 178
	private $_baseUrl;

Qiang Xue committed
179 180 181 182 183 184
	/**
	 * 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.
	 */
Qiang Xue committed
185
	public function getBaseUrl()
Qiang Xue committed
186
	{
Qiang Xue committed
187 188 189 190 191
		if ($this->_baseUrl === null) {
			/** @var $request \yii\web\Request */
			$request = Yii::$app->getRequest();
			$this->_baseUrl = $this->showScriptName ? $request->getScriptUrl() : $request->getBaseUrl();
		}
Qiang Xue committed
192 193
		return $this->_baseUrl;
	}
Qiang Xue committed
194

Qiang Xue committed
195 196 197
	public function setBaseUrl($value)
	{
		$this->_baseUrl = trim($value, '/');
Qiang Xue committed
198
	}
Qiang Xue committed
199

Qiang Xue committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
	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;
	}

Qiang Xue committed
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
	/**
	 * Removes the URL suffix from path info.
	 * @param string $pathInfo path info part in the URL
	 * @param string $suffix the URL suffix to be removed
	 * @return string path info with URL suffix removed.
	 */
	public function removeSuffix($pathInfo, $suffix)
	{
		$n = strlen($suffix);
		if ($n > 0 && substr($pathInfo, -$n) === $suffix) {
			return substr($pathInfo, 0, -$n);
		} else {
			return $pathInfo;
		}
	}
Qiang Xue committed
232
}