UrlRule.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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\rest;
  8. use Yii;
  9. use yii\base\InvalidConfigException;
  10. use yii\helpers\Inflector;
  11. use yii\web\CompositeUrlRule;
  12. use yii\web\UrlRule as WebUrlRule;
  13. use yii\web\UrlRuleInterface;
  14. /**
  15. * UrlRule is provided to simplify the creation of URL rules for RESTful API support.
  16. *
  17. * The simplest usage of UrlRule is to declare a rule like the following in the application configuration,
  18. *
  19. * ```php
  20. * [
  21. * 'class' => 'yii\rest\UrlRule',
  22. * 'controller' => 'user',
  23. * ]
  24. * ```
  25. *
  26. * The above code will create a whole set of URL rules supporting the following RESTful API endpoints:
  27. *
  28. * - `'PUT,PATCH users/<id>' => 'user/update'`: update a user
  29. * - `'DELETE users/<id>' => 'user/delete'`: delete a user
  30. * - `'GET,HEAD users/<id>' => 'user/view'`: return the details/overview/options of a user
  31. * - `'POST users' => 'user/create'`: create a new user
  32. * - `'GET,HEAD users' => 'user/index'`: return a list/overview/options of users
  33. * - `'users/<id>' => 'user/options'`: process all unhandled verbs of a user
  34. * - `'users' => 'user/options'`: process all unhandled verbs of user collection
  35. *
  36. * You may configure [[only]] and/or [[except]] to disable some of the above rules.
  37. * You may configure [[patterns]] to completely redefine your own list of rules.
  38. * You may configure [[controller]] with multiple controller IDs to generate rules for all these controllers.
  39. * For example, the following code will disable the `delete` rule and generate rules for both `user` and `post` controllers:
  40. *
  41. * ```php
  42. * [
  43. * 'class' => 'yii\rest\UrlRule',
  44. * 'controller' => ['user', 'post'],
  45. * 'except' => ['delete'],
  46. * ]
  47. * ```
  48. *
  49. * The property [[controller]] is required and should represent one or multiple controller IDs.
  50. * Each controller ID should be prefixed with the module ID if the controller is within a module.
  51. * The controller ID used in the pattern will be automatically pluralized (e.g. `user` becomes `users`
  52. * as shown in the above examples).
  53. *
  54. * For more details and usage information on UrlRule, see the [guide article on rest routing](guide:rest-routing).
  55. *
  56. * @author Qiang Xue <qiang.xue@gmail.com>
  57. * @since 2.0
  58. */
  59. class UrlRule extends CompositeUrlRule
  60. {
  61. /**
  62. * @var string the common prefix string shared by all patterns.
  63. */
  64. public $prefix;
  65. /**
  66. * @var string the suffix that will be assigned to [[\yii\web\UrlRule::suffix]] for every generated rule.
  67. */
  68. public $suffix;
  69. /**
  70. * @var string|array the controller ID (e.g. `user`, `post-comment`) that the rules in this composite rule
  71. * are dealing with. It should be prefixed with the module ID if the controller is within a module (e.g. `admin/user`).
  72. *
  73. * By default, the controller ID will be pluralized automatically when it is put in the patterns of the
  74. * generated rules. If you want to explicitly specify how the controller ID should appear in the patterns,
  75. * you may use an array with the array key being as the controller ID in the pattern, and the array value
  76. * the actual controller ID. For example, `['u' => 'user']`.
  77. *
  78. * You may also pass multiple controller IDs as an array. If this is the case, this composite rule will
  79. * generate applicable URL rules for EVERY specified controller. For example, `['user', 'post']`.
  80. */
  81. public $controller;
  82. /**
  83. * @var array list of acceptable actions. If not empty, only the actions within this array
  84. * will have the corresponding URL rules created.
  85. * @see patterns
  86. */
  87. public $only = [];
  88. /**
  89. * @var array list of actions that should be excluded. Any action found in this array
  90. * will NOT have its URL rules created.
  91. * @see patterns
  92. */
  93. public $except = [];
  94. /**
  95. * @var array patterns for supporting extra actions in addition to those listed in [[patterns]].
  96. * The keys are the patterns and the values are the corresponding action IDs.
  97. * These extra patterns will take precedence over [[patterns]].
  98. */
  99. public $extraPatterns = [];
  100. /**
  101. * @var array list of tokens that should be replaced for each pattern. The keys are the token names,
  102. * and the values are the corresponding replacements.
  103. * @see patterns
  104. */
  105. public $tokens = [
  106. '{id}' => '<id:\\d[\\d,]*>',
  107. ];
  108. /**
  109. * @var array list of possible patterns and the corresponding actions for creating the URL rules.
  110. * The keys are the patterns and the values are the corresponding actions.
  111. * The format of patterns is `Verbs Pattern`, where `Verbs` stands for a list of HTTP verbs separated
  112. * by comma (without space). If `Verbs` is not specified, it means all verbs are allowed.
  113. * `Pattern` is optional. It will be prefixed with [[prefix]]/[[controller]]/,
  114. * and tokens in it will be replaced by [[tokens]].
  115. */
  116. public $patterns = [
  117. 'PUT,PATCH {id}' => 'update',
  118. 'DELETE {id}' => 'delete',
  119. 'GET,HEAD {id}' => 'view',
  120. 'POST' => 'create',
  121. 'GET,HEAD' => 'index',
  122. '{id}' => 'options',
  123. '' => 'options',
  124. ];
  125. /**
  126. * @var array the default configuration for creating each URL rule contained by this rule.
  127. */
  128. public $ruleConfig = [
  129. 'class' => 'yii\web\UrlRule',
  130. ];
  131. /**
  132. * @var bool whether to automatically pluralize the URL names for controllers.
  133. * If true, a controller ID will appear in plural form in URLs. For example, `user` controller
  134. * will appear as `users` in URLs.
  135. * @see controller
  136. */
  137. public $pluralize = true;
  138. /**
  139. * {@inheritdoc}
  140. */
  141. public function init()
  142. {
  143. if (empty($this->controller)) {
  144. throw new InvalidConfigException('"controller" must be set.');
  145. }
  146. $controllers = [];
  147. foreach ((array) $this->controller as $urlName => $controller) {
  148. if (is_int($urlName)) {
  149. $urlName = $this->pluralize ? Inflector::pluralize($controller) : $controller;
  150. }
  151. $controllers[$urlName] = $controller;
  152. }
  153. $this->controller = $controllers;
  154. $this->prefix = trim($this->prefix, '/');
  155. parent::init();
  156. }
  157. /**
  158. * {@inheritdoc}
  159. */
  160. protected function createRules()
  161. {
  162. $only = array_flip($this->only);
  163. $except = array_flip($this->except);
  164. $patterns = $this->extraPatterns + $this->patterns;
  165. $rules = [];
  166. foreach ($this->controller as $urlName => $controller) {
  167. $prefix = trim($this->prefix . '/' . $urlName, '/');
  168. foreach ($patterns as $pattern => $action) {
  169. if (!isset($except[$action]) && (empty($only) || isset($only[$action]))) {
  170. $rules[$urlName][] = $this->createRule($pattern, $prefix, $controller . '/' . $action);
  171. }
  172. }
  173. }
  174. return $rules;
  175. }
  176. /**
  177. * Creates a URL rule using the given pattern and action.
  178. * @param string $pattern
  179. * @param string $prefix
  180. * @param string $action
  181. * @return UrlRuleInterface
  182. */
  183. protected function createRule($pattern, $prefix, $action)
  184. {
  185. $verbs = 'GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS';
  186. if (preg_match("/^((?:($verbs),)*($verbs))(?:\\s+(.*))?$/", $pattern, $matches)) {
  187. $verbs = explode(',', $matches[1]);
  188. $pattern = isset($matches[4]) ? $matches[4] : '';
  189. } else {
  190. $verbs = [];
  191. }
  192. $config = $this->ruleConfig;
  193. $config['verb'] = $verbs;
  194. $config['pattern'] = rtrim($prefix . '/' . strtr($pattern, $this->tokens), '/');
  195. $config['route'] = $action;
  196. if (!empty($verbs) && !in_array('GET', $verbs)) {
  197. $config['mode'] = WebUrlRule::PARSING_ONLY;
  198. }
  199. $config['suffix'] = $this->suffix;
  200. return Yii::createObject($config);
  201. }
  202. /**
  203. * {@inheritdoc}
  204. */
  205. public function parseRequest($manager, $request)
  206. {
  207. $pathInfo = $request->getPathInfo();
  208. foreach ($this->rules as $urlName => $rules) {
  209. if (strpos($pathInfo, $urlName) !== false) {
  210. foreach ($rules as $rule) {
  211. /* @var $rule WebUrlRule */
  212. $result = $rule->parseRequest($manager, $request);
  213. if (YII_DEBUG) {
  214. Yii::debug([
  215. 'rule' => method_exists($rule, '__toString') ? $rule->__toString() : get_class($rule),
  216. 'match' => $result !== false,
  217. 'parent' => self::className(),
  218. ], __METHOD__);
  219. }
  220. if ($result !== false) {
  221. return $result;
  222. }
  223. }
  224. }
  225. }
  226. return false;
  227. }
  228. /**
  229. * {@inheritdoc}
  230. */
  231. public function createUrl($manager, $route, $params)
  232. {
  233. $this->createStatus = WebUrlRule::CREATE_STATUS_SUCCESS;
  234. foreach ($this->controller as $urlName => $controller) {
  235. if (strpos($route, $controller) !== false) {
  236. /* @var $rules UrlRuleInterface[] */
  237. $rules = $this->rules[$urlName];
  238. $url = $this->iterateRules($rules, $manager, $route, $params);
  239. if ($url !== false) {
  240. return $url;
  241. }
  242. } else {
  243. $this->createStatus |= WebUrlRule::CREATE_STATUS_ROUTE_MISMATCH;
  244. }
  245. }
  246. if ($this->createStatus === WebUrlRule::CREATE_STATUS_SUCCESS) {
  247. // create status was not changed - there is no rules configured
  248. $this->createStatus = WebUrlRule::CREATE_STATUS_PARSING_ONLY;
  249. }
  250. return false;
  251. }
  252. }