Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

464 Zeilen
18KB

  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\web;
  8. use Yii;
  9. use yii\base\Component;
  10. use yii\base\InvalidConfigException;
  11. use yii\caching\Cache;
  12. /**
  13. * UrlManager handles HTTP request parsing and creation of URLs based on a set of rules.
  14. *
  15. * UrlManager is configured as an application component in [[\yii\base\Application]] by default.
  16. * You can access that instance via `Yii::$app->urlManager`.
  17. *
  18. * You can modify its configuration by adding an array to your application config under `components`
  19. * as it is shown in the following example:
  20. *
  21. * ~~~
  22. * 'urlManager' => [
  23. * 'enablePrettyUrl' => true,
  24. * 'rules' => [
  25. * // your rules go here
  26. * ],
  27. * // ...
  28. * ]
  29. * ~~~
  30. *
  31. * @property string $baseUrl The base URL that is used by [[createUrl()]] to prepend to created URLs.
  32. * @property string $hostInfo The host info (e.g. "http://www.example.com") that is used by
  33. * [[createAbsoluteUrl()]] to prepend to created URLs.
  34. * @property string $scriptUrl The entry script URL that is used by [[createUrl()]] to prepend to created
  35. * URLs.
  36. *
  37. * @author Qiang Xue <qiang.xue@gmail.com>
  38. * @since 2.0
  39. */
  40. class UrlManager extends Component
  41. {
  42. /**
  43. * @var boolean whether to enable pretty URLs. Instead of putting all parameters in the query
  44. * string part of a URL, pretty URLs allow using path info to represent some of the parameters
  45. * and can thus produce more user-friendly URLs, such as "/news/Yii-is-released", instead of
  46. * "/index.php?r=news/view&id=100".
  47. */
  48. public $enablePrettyUrl = false;
  49. /**
  50. * @var boolean whether to enable strict parsing. If strict parsing is enabled, the incoming
  51. * requested URL must match at least one of the [[rules]] in order to be treated as a valid request.
  52. * Otherwise, the path info part of the request will be treated as the requested route.
  53. * This property is used only when [[enablePrettyUrl]] is true.
  54. */
  55. public $enableStrictParsing = false;
  56. /**
  57. * @var array the rules for creating and parsing URLs when [[enablePrettyUrl]] is true.
  58. * This property is used only if [[enablePrettyUrl]] is true. Each element in the array
  59. * is the configuration array for creating a single URL rule. The configuration will
  60. * be merged with [[ruleConfig]] first before it is used for creating the rule object.
  61. *
  62. * A special shortcut format can be used if a rule only specifies [[UrlRule::pattern|pattern]]
  63. * and [[UrlRule::route|route]]: `'pattern' => 'route'`. That is, instead of using a configuration
  64. * array, one can use the key to represent the pattern and the value the corresponding route.
  65. * For example, `'post/<id:\d+>' => 'post/view'`.
  66. *
  67. * For RESTful routing the mentioned shortcut format also allows you to specify the
  68. * [[UrlRule::verb|HTTP verb]] that the rule should apply for.
  69. * You can do that by prepending it to the pattern, separated by space.
  70. * For example, `'PUT post/<id:\d+>' => 'post/update'`.
  71. * You may specify multiple verbs by separating them with comma
  72. * like this: `'POST,PUT post/index' => 'post/create'`.
  73. * The supported verbs in the shortcut format are: GET, HEAD, POST, PUT, PATCH and DELETE.
  74. * Note that [[UrlRule::mode|mode]] will be set to PARSING_ONLY when specifying verb in this way
  75. * so you normally would not specify a verb for normal GET request.
  76. *
  77. * Here is an example configuration for RESTful CRUD controller:
  78. *
  79. * ~~~php
  80. * [
  81. * 'dashboard' => 'site/index',
  82. *
  83. * 'POST <controller:\w+>s' => '<controller>/create',
  84. * '<controller:\w+>s' => '<controller>/index',
  85. *
  86. * 'PUT <controller:\w+>/<id:\d+>' => '<controller>/update',
  87. * 'DELETE <controller:\w+>/<id:\d+>' => '<controller>/delete',
  88. * '<controller:\w+>/<id:\d+>' => '<controller>/view',
  89. * ];
  90. * ~~~
  91. *
  92. * Note that if you modify this property after the UrlManager object is created, make sure
  93. * you populate the array with rule objects instead of rule configurations.
  94. */
  95. public $rules = [];
  96. /**
  97. * @var string the URL suffix used when in 'path' format.
  98. * For example, ".html" can be used so that the URL looks like pointing to a static HTML page.
  99. * This property is used only if [[enablePrettyUrl]] is true.
  100. */
  101. public $suffix;
  102. /**
  103. * @var boolean whether to show entry script name in the constructed URL. Defaults to true.
  104. * This property is used only if [[enablePrettyUrl]] is true.
  105. */
  106. public $showScriptName = true;
  107. /**
  108. * @var string the GET parameter name for route. This property is used only if [[enablePrettyUrl]] is false.
  109. */
  110. public $routeParam = 'r';
  111. /**
  112. * @var Cache|string the cache object or the application component ID of the cache object.
  113. * Compiled URL rules will be cached through this cache object, if it is available.
  114. *
  115. * After the UrlManager object is created, if you want to change this property,
  116. * you should only assign it with a cache object.
  117. * Set this property to false if you do not want to cache the URL rules.
  118. */
  119. public $cache = 'cache';
  120. /**
  121. * @var array the default configuration of URL rules. Individual rule configurations
  122. * specified via [[rules]] will take precedence when the same property of the rule is configured.
  123. */
  124. public $ruleConfig = ['class' => 'yii\web\UrlRule'];
  125. private $_baseUrl;
  126. private $_scriptUrl;
  127. private $_hostInfo;
  128. /**
  129. * Initializes UrlManager.
  130. */
  131. public function init()
  132. {
  133. parent::init();
  134. if (!$this->enablePrettyUrl || empty($this->rules)) {
  135. return;
  136. }
  137. if (is_string($this->cache)) {
  138. $this->cache = Yii::$app->get($this->cache, false);
  139. }
  140. if ($this->cache instanceof Cache) {
  141. $cacheKey = __CLASS__;
  142. $hash = md5(json_encode($this->rules));
  143. if (($data = $this->cache->get($cacheKey)) !== false && isset($data[1]) && $data[1] === $hash) {
  144. $this->rules = $data[0];
  145. } else {
  146. $this->rules = $this->buildRules($this->rules);
  147. $this->cache->set($cacheKey, [$this->rules, $hash]);
  148. }
  149. } else {
  150. $this->rules = $this->buildRules($this->rules);
  151. }
  152. }
  153. /**
  154. * Adds additional URL rules.
  155. *
  156. * This method will call [[buildRules()]] to parse the given rule declarations and then append or insert
  157. * them to the existing [[rules]].
  158. *
  159. * Note that if [[enablePrettyUrl]] is false, this method will do nothing.
  160. *
  161. * @param array $rules the new rules to be added. Each array element represents a single rule declaration.
  162. * Please refer to [[rules]] for the acceptable rule format.
  163. * @param boolean $append whether to add the new rules by appending them to the end of the existing rules.
  164. */
  165. public function addRules($rules, $append = true)
  166. {
  167. if (!$this->enablePrettyUrl) {
  168. return;
  169. }
  170. $rules = $this->buildRules($rules);
  171. if ($append) {
  172. $this->rules = array_merge($this->rules, $rules);
  173. } else {
  174. $this->rules = array_merge($rules, $this->rules);
  175. }
  176. }
  177. /**
  178. * Builds URL rule objects from the given rule declarations.
  179. * @param array $rules the rule declarations. Each array element represents a single rule declaration.
  180. * Please refer to [[rules]] for the acceptable rule formats.
  181. * @return UrlRuleInterface[] the rule objects built from the given rule declarations
  182. * @throws InvalidConfigException if a rule declaration is invalid
  183. */
  184. protected function buildRules($rules)
  185. {
  186. $compiledRules = [];
  187. $verbs = 'GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS';
  188. foreach ($rules as $key => $rule) {
  189. if (is_string($rule)) {
  190. $rule = ['route' => $rule];
  191. if (preg_match("/^((?:($verbs),)*($verbs))\\s+(.*)$/", $key, $matches)) {
  192. $rule['verb'] = explode(',', $matches[1]);
  193. // rules that do not apply for GET requests should not be use to create urls
  194. if (!in_array('GET', $rule['verb'])) {
  195. $rule['mode'] = UrlRule::PARSING_ONLY;
  196. }
  197. $key = $matches[4];
  198. }
  199. $rule['pattern'] = $key;
  200. }
  201. if (is_array($rule)) {
  202. $rule = Yii::createObject(array_merge($this->ruleConfig, $rule));
  203. }
  204. if (!$rule instanceof UrlRuleInterface) {
  205. throw new InvalidConfigException('URL rule class must implement UrlRuleInterface.');
  206. }
  207. $compiledRules[] = $rule;
  208. }
  209. return $compiledRules;
  210. }
  211. /**
  212. * Parses the user request.
  213. * @param Request $request the request component
  214. * @return array|boolean the route and the associated parameters. The latter is always empty
  215. * if [[enablePrettyUrl]] is false. False is returned if the current request cannot be successfully parsed.
  216. */
  217. public function parseRequest($request)
  218. {
  219. if ($this->enablePrettyUrl) {
  220. $pathInfo = $request->getPathInfo();
  221. /* @var $rule UrlRule */
  222. foreach ($this->rules as $rule) {
  223. if (($result = $rule->parseRequest($this, $request)) !== false) {
  224. return $result;
  225. }
  226. }
  227. if ($this->enableStrictParsing) {
  228. return false;
  229. }
  230. Yii::trace('No matching URL rules. Using default URL parsing logic.', __METHOD__);
  231. $suffix = (string) $this->suffix;
  232. if ($suffix !== '' && $pathInfo !== '') {
  233. $n = strlen($this->suffix);
  234. if (substr_compare($pathInfo, $this->suffix, -$n, $n) === 0) {
  235. $pathInfo = substr($pathInfo, 0, -$n);
  236. if ($pathInfo === '') {
  237. // suffix alone is not allowed
  238. return false;
  239. }
  240. } else {
  241. // suffix doesn't match
  242. return false;
  243. }
  244. }
  245. return [$pathInfo, []];
  246. } else {
  247. Yii::trace('Pretty URL not enabled. Using default URL parsing logic.', __METHOD__);
  248. $route = $request->getQueryParam($this->routeParam, '');
  249. if (is_array($route)) {
  250. $route = '';
  251. }
  252. return [(string) $route, []];
  253. }
  254. }
  255. /**
  256. * Creates a URL using the given route and query parameters.
  257. *
  258. * You may specify the route as a string, e.g., `site/index`. You may also use an array
  259. * if you want to specify additional query parameters for the URL being created. The
  260. * array format must be:
  261. *
  262. * ```php
  263. * // generates: /index.php?r=site/index&param1=value1&param2=value2
  264. * ['site/index', 'param1' => 'value1', 'param2' => 'value2']
  265. * ```
  266. *
  267. * If you want to create a URL with an anchor, you can use the array format with a `#` parameter.
  268. * For example,
  269. *
  270. * ```php
  271. * // generates: /index.php?r=site/index&param1=value1#name
  272. * ['site/index', 'param1' => 'value1', '#' => 'name']
  273. * ```
  274. *
  275. * The URL created is a relative one. Use [[createAbsoluteUrl()]] to create an absolute URL.
  276. *
  277. * Note that unlike [[\yii\helpers\Url::toRoute()]], this method always treats the given route
  278. * as an absolute route.
  279. *
  280. * @param string|array $params use a string to represent a route (e.g. `site/index`),
  281. * or an array to represent a route with query parameters (e.g. `['site/index', 'param1' => 'value1']`).
  282. * @return string the created URL
  283. */
  284. public function createUrl($params)
  285. {
  286. $params = (array) $params;
  287. $anchor = isset($params['#']) ? '#' . $params['#'] : '';
  288. unset($params['#'], $params[$this->routeParam]);
  289. $route = trim($params[0], '/');
  290. unset($params[0]);
  291. $baseUrl = $this->showScriptName || !$this->enablePrettyUrl ? $this->getScriptUrl() : $this->getBaseUrl();
  292. if ($this->enablePrettyUrl) {
  293. /* @var $rule UrlRule */
  294. foreach ($this->rules as $rule) {
  295. if (($url = $rule->createUrl($this, $route, $params)) !== false) {
  296. if (strpos($url, '://') !== false) {
  297. if ($baseUrl !== '' && ($pos = strpos($url, '/', 8)) !== false) {
  298. return substr($url, 0, $pos) . $baseUrl . substr($url, $pos);
  299. } else {
  300. return $url . $baseUrl . $anchor;
  301. }
  302. } else {
  303. return "$baseUrl/{$url}{$anchor}";
  304. }
  305. }
  306. }
  307. if ($this->suffix !== null) {
  308. $route .= $this->suffix;
  309. }
  310. if (!empty($params) && ($query = http_build_query($params)) !== '') {
  311. $route .= '?' . $query;
  312. }
  313. return "$baseUrl/{$route}{$anchor}";
  314. } else {
  315. $url = "$baseUrl?{$this->routeParam}=" . urlencode($route);
  316. if (!empty($params) && ($query = http_build_query($params)) !== '') {
  317. $url .= '&' . $query;
  318. }
  319. return $url . $anchor;
  320. }
  321. }
  322. /**
  323. * Creates an absolute URL using the given route and query parameters.
  324. *
  325. * This method prepends the URL created by [[createUrl()]] with the [[hostInfo]].
  326. *
  327. * Note that unlike [[\yii\helpers\Url::toRoute()]], this method always treats the given route
  328. * as an absolute route.
  329. *
  330. * @param string|array $params use a string to represent a route (e.g. `site/index`),
  331. * or an array to represent a route with query parameters (e.g. `['site/index', 'param1' => 'value1']`).
  332. * @param string $scheme the scheme to use for the url (either `http` or `https`). If not specified
  333. * the scheme of the current request will be used.
  334. * @return string the created URL
  335. * @see createUrl()
  336. */
  337. public function createAbsoluteUrl($params, $scheme = null)
  338. {
  339. $params = (array) $params;
  340. $url = $this->createUrl($params);
  341. if (strpos($url, '://') === false) {
  342. $url = $this->getHostInfo() . $url;
  343. }
  344. if (is_string($scheme) && ($pos = strpos($url, '://')) !== false) {
  345. $url = $scheme . substr($url, $pos);
  346. }
  347. return $url;
  348. }
  349. /**
  350. * Returns the base URL that is used by [[createUrl()]] to prepend to created URLs.
  351. * It defaults to [[Request::baseUrl]].
  352. * This is mainly used when [[enablePrettyUrl]] is true and [[showScriptName]] is false.
  353. * @return string the base URL that is used by [[createUrl()]] to prepend to created URLs.
  354. * @throws InvalidConfigException if running in console application and [[baseUrl]] is not configured.
  355. */
  356. public function getBaseUrl()
  357. {
  358. if ($this->_baseUrl === null) {
  359. $request = Yii::$app->getRequest();
  360. if ($request instanceof Request) {
  361. $this->_baseUrl = $request->getBaseUrl();
  362. } else {
  363. throw new InvalidConfigException('Please configure UrlManager::baseUrl correctly as you are running a console application.');
  364. }
  365. }
  366. return $this->_baseUrl;
  367. }
  368. /**
  369. * Sets the base URL that is used by [[createUrl()]] to prepend to created URLs.
  370. * This is mainly used when [[enablePrettyUrl]] is true and [[showScriptName]] is false.
  371. * @param string $value the base URL that is used by [[createUrl()]] to prepend to created URLs.
  372. */
  373. public function setBaseUrl($value)
  374. {
  375. $this->_baseUrl = rtrim($value, '/');
  376. }
  377. /**
  378. * Returns the entry script URL that is used by [[createUrl()]] to prepend to created URLs.
  379. * It defaults to [[Request::scriptUrl]].
  380. * This is mainly used when [[enablePrettyUrl]] is false or [[showScriptName]] is true.
  381. * @return string the entry script URL that is used by [[createUrl()]] to prepend to created URLs.
  382. * @throws InvalidConfigException if running in console application and [[scriptUrl]] is not configured.
  383. */
  384. public function getScriptUrl()
  385. {
  386. if ($this->_scriptUrl === null) {
  387. $request = Yii::$app->getRequest();
  388. if ($request instanceof Request) {
  389. $this->_scriptUrl = $request->getScriptUrl();
  390. } else {
  391. throw new InvalidConfigException('Please configure UrlManager::scriptUrl correctly as you are running a console application.');
  392. }
  393. }
  394. return $this->_scriptUrl;
  395. }
  396. /**
  397. * Sets the entry script URL that is used by [[createUrl()]] to prepend to created URLs.
  398. * This is mainly used when [[enablePrettyUrl]] is false or [[showScriptName]] is true.
  399. * @param string $value the entry script URL that is used by [[createUrl()]] to prepend to created URLs.
  400. */
  401. public function setScriptUrl($value)
  402. {
  403. $this->_scriptUrl = $value;
  404. }
  405. /**
  406. * Returns the host info that is used by [[createAbsoluteUrl()]] to prepend to created URLs.
  407. * @return string the host info (e.g. "http://www.example.com") that is used by [[createAbsoluteUrl()]] to prepend to created URLs.
  408. * @throws InvalidConfigException if running in console application and [[hostInfo]] is not configured.
  409. */
  410. public function getHostInfo()
  411. {
  412. if ($this->_hostInfo === null) {
  413. $request = Yii::$app->getRequest();
  414. if ($request instanceof \yii\web\Request) {
  415. $this->_hostInfo = $request->getHostInfo();
  416. } else {
  417. throw new InvalidConfigException('Please configure UrlManager::hostInfo correctly as you are running a console application.');
  418. }
  419. }
  420. return $this->_hostInfo;
  421. }
  422. /**
  423. * Sets the host info that is used by [[createAbsoluteUrl()]] to prepend to created URLs.
  424. * @param string $value the host info (e.g. "http://www.example.com") that is used by [[createAbsoluteUrl()]] to prepend to created URLs.
  425. */
  426. public function setHostInfo($value)
  427. {
  428. $this->_hostInfo = rtrim($value, '/');
  429. }
  430. }