In this repo i store all my websites, each in a different branch
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

313 lines
11 KiB

  1. FastRoute - Fast request router for PHP
  2. =======================================
  3. This library provides a fast implementation of a regular expression based router. [Blog post explaining how the
  4. implementation works and why it is fast.][blog_post]
  5. Install
  6. -------
  7. To install with composer:
  8. ```sh
  9. composer require nikic/fast-route
  10. ```
  11. Requires PHP 5.4 or newer.
  12. Usage
  13. -----
  14. Here's a basic usage example:
  15. ```php
  16. <?php
  17. require '/path/to/vendor/autoload.php';
  18. $dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) {
  19. $r->addRoute('GET', '/users', 'get_all_users_handler');
  20. // {id} must be a number (\d+)
  21. $r->addRoute('GET', '/user/{id:\d+}', 'get_user_handler');
  22. // The /{title} suffix is optional
  23. $r->addRoute('GET', '/articles/{id:\d+}[/{title}]', 'get_article_handler');
  24. });
  25. // Fetch method and URI from somewhere
  26. $httpMethod = $_SERVER['REQUEST_METHOD'];
  27. $uri = $_SERVER['REQUEST_URI'];
  28. // Strip query string (?foo=bar) and decode URI
  29. if (false !== $pos = strpos($uri, '?')) {
  30. $uri = substr($uri, 0, $pos);
  31. }
  32. $uri = rawurldecode($uri);
  33. $routeInfo = $dispatcher->dispatch($httpMethod, $uri);
  34. switch ($routeInfo[0]) {
  35. case FastRoute\Dispatcher::NOT_FOUND:
  36. // ... 404 Not Found
  37. break;
  38. case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
  39. $allowedMethods = $routeInfo[1];
  40. // ... 405 Method Not Allowed
  41. break;
  42. case FastRoute\Dispatcher::FOUND:
  43. $handler = $routeInfo[1];
  44. $vars = $routeInfo[2];
  45. // ... call $handler with $vars
  46. break;
  47. }
  48. ```
  49. ### Defining routes
  50. The routes are defined by calling the `FastRoute\simpleDispatcher()` function, which accepts
  51. a callable taking a `FastRoute\RouteCollector` instance. The routes are added by calling
  52. `addRoute()` on the collector instance:
  53. ```php
  54. $r->addRoute($method, $routePattern, $handler);
  55. ```
  56. The `$method` is an uppercase HTTP method string for which a certain route should match. It
  57. is possible to specify multiple valid methods using an array:
  58. ```php
  59. // These two calls
  60. $r->addRoute('GET', '/test', 'handler');
  61. $r->addRoute('POST', '/test', 'handler');
  62. // Are equivalent to this one call
  63. $r->addRoute(['GET', 'POST'], '/test', 'handler');
  64. ```
  65. By default the `$routePattern` uses a syntax where `{foo}` specifies a placeholder with name `foo`
  66. and matching the regex `[^/]+`. To adjust the pattern the placeholder matches, you can specify
  67. a custom pattern by writing `{bar:[0-9]+}`. Some examples:
  68. ```php
  69. // Matches /user/42, but not /user/xyz
  70. $r->addRoute('GET', '/user/{id:\d+}', 'handler');
  71. // Matches /user/foobar, but not /user/foo/bar
  72. $r->addRoute('GET', '/user/{name}', 'handler');
  73. // Matches /user/foo/bar as well
  74. $r->addRoute('GET', '/user/{name:.+}', 'handler');
  75. ```
  76. Custom patterns for route placeholders cannot use capturing groups. For example `{lang:(en|de)}`
  77. is not a valid placeholder, because `()` is a capturing group. Instead you can use either
  78. `{lang:en|de}` or `{lang:(?:en|de)}`.
  79. Furthermore parts of the route enclosed in `[...]` are considered optional, so that `/foo[bar]`
  80. will match both `/foo` and `/foobar`. Optional parts are only supported in a trailing position,
  81. not in the middle of a route.
  82. ```php
  83. // This route
  84. $r->addRoute('GET', '/user/{id:\d+}[/{name}]', 'handler');
  85. // Is equivalent to these two routes
  86. $r->addRoute('GET', '/user/{id:\d+}', 'handler');
  87. $r->addRoute('GET', '/user/{id:\d+}/{name}', 'handler');
  88. // Multiple nested optional parts are possible as well
  89. $r->addRoute('GET', '/user[/{id:\d+}[/{name}]]', 'handler');
  90. // This route is NOT valid, because optional parts can only occur at the end
  91. $r->addRoute('GET', '/user[/{id:\d+}]/{name}', 'handler');
  92. ```
  93. The `$handler` parameter does not necessarily have to be a callback, it could also be a controller
  94. class name or any other kind of data you wish to associate with the route. FastRoute only tells you
  95. which handler corresponds to your URI, how you interpret it is up to you.
  96. #### Shorcut methods for common request methods
  97. For the `GET`, `POST`, `PUT`, `PATCH`, `DELETE` and `HEAD` request methods shortcut methods are available. For example:
  98. ```php
  99. $r->get('/get-route', 'get_handler');
  100. $r->post('/post-route', 'post_handler');
  101. ```
  102. Is equivalent to:
  103. ```php
  104. $r->addRoute('GET', '/get-route', 'get_handler');
  105. $r->addRoute('POST', '/post-route', 'post_handler');
  106. ```
  107. #### Route Groups
  108. Additionally, you can specify routes inside of a group. All routes defined inside a group will have a common prefix.
  109. For example, defining your routes as:
  110. ```php
  111. $r->addGroup('/admin', function (RouteCollector $r) {
  112. $r->addRoute('GET', '/do-something', 'handler');
  113. $r->addRoute('GET', '/do-another-thing', 'handler');
  114. $r->addRoute('GET', '/do-something-else', 'handler');
  115. });
  116. ```
  117. Will have the same result as:
  118. ```php
  119. $r->addRoute('GET', '/admin/do-something', 'handler');
  120. $r->addRoute('GET', '/admin/do-another-thing', 'handler');
  121. $r->addRoute('GET', '/admin/do-something-else', 'handler');
  122. ```
  123. Nested groups are also supported, in which case the prefixes of all the nested groups are combined.
  124. ### Caching
  125. The reason `simpleDispatcher` accepts a callback for defining the routes is to allow seamless
  126. caching. By using `cachedDispatcher` instead of `simpleDispatcher` you can cache the generated
  127. routing data and construct the dispatcher from the cached information:
  128. ```php
  129. <?php
  130. $dispatcher = FastRoute\cachedDispatcher(function(FastRoute\RouteCollector $r) {
  131. $r->addRoute('GET', '/user/{name}/{id:[0-9]+}', 'handler0');
  132. $r->addRoute('GET', '/user/{id:[0-9]+}', 'handler1');
  133. $r->addRoute('GET', '/user/{name}', 'handler2');
  134. }, [
  135. 'cacheFile' => __DIR__ . '/route.cache', /* required */
  136. 'cacheDisabled' => IS_DEBUG_ENABLED, /* optional, enabled by default */
  137. ]);
  138. ```
  139. The second parameter to the function is an options array, which can be used to specify the cache
  140. file location, among other things.
  141. ### Dispatching a URI
  142. A URI is dispatched by calling the `dispatch()` method of the created dispatcher. This method
  143. accepts the HTTP method and a URI. Getting those two bits of information (and normalizing them
  144. appropriately) is your job - this library is not bound to the PHP web SAPIs.
  145. The `dispatch()` method returns an array whose first element contains a status code. It is one
  146. of `Dispatcher::NOT_FOUND`, `Dispatcher::METHOD_NOT_ALLOWED` and `Dispatcher::FOUND`. For the
  147. method not allowed status the second array element contains a list of HTTP methods allowed for
  148. the supplied URI. For example:
  149. [FastRoute\Dispatcher::METHOD_NOT_ALLOWED, ['GET', 'POST']]
  150. > **NOTE:** The HTTP specification requires that a `405 Method Not Allowed` response include the
  151. `Allow:` header to detail available methods for the requested resource. Applications using FastRoute
  152. should use the second array element to add this header when relaying a 405 response.
  153. For the found status the second array element is the handler that was associated with the route
  154. and the third array element is a dictionary of placeholder names to their values. For example:
  155. /* Routing against GET /user/nikic/42 */
  156. [FastRoute\Dispatcher::FOUND, 'handler0', ['name' => 'nikic', 'id' => '42']]
  157. ### Overriding the route parser and dispatcher
  158. The routing process makes use of three components: A route parser, a data generator and a
  159. dispatcher. The three components adhere to the following interfaces:
  160. ```php
  161. <?php
  162. namespace FastRoute;
  163. interface RouteParser {
  164. public function parse($route);
  165. }
  166. interface DataGenerator {
  167. public function addRoute($httpMethod, $routeData, $handler);
  168. public function getData();
  169. }
  170. interface Dispatcher {
  171. const NOT_FOUND = 0, FOUND = 1, METHOD_NOT_ALLOWED = 2;
  172. public function dispatch($httpMethod, $uri);
  173. }
  174. ```
  175. The route parser takes a route pattern string and converts it into an array of route infos, where
  176. each route info is again an array of it's parts. The structure is best understood using an example:
  177. /* The route /user/{id:\d+}[/{name}] converts to the following array: */
  178. [
  179. [
  180. '/user/',
  181. ['id', '\d+'],
  182. ],
  183. [
  184. '/user/',
  185. ['id', '\d+'],
  186. '/',
  187. ['name', '[^/]+'],
  188. ],
  189. ]
  190. This array can then be passed to the `addRoute()` method of a data generator. After all routes have
  191. been added the `getData()` of the generator is invoked, which returns all the routing data required
  192. by the dispatcher. The format of this data is not further specified - it is tightly coupled to
  193. the corresponding dispatcher.
  194. The dispatcher accepts the routing data via a constructor and provides a `dispatch()` method, which
  195. you're already familiar with.
  196. The route parser can be overwritten individually (to make use of some different pattern syntax),
  197. however the data generator and dispatcher should always be changed as a pair, as the output from
  198. the former is tightly coupled to the input of the latter. The reason the generator and the
  199. dispatcher are separate is that only the latter is needed when using caching (as the output of
  200. the former is what is being cached.)
  201. When using the `simpleDispatcher` / `cachedDispatcher` functions from above the override happens
  202. through the options array:
  203. ```php
  204. <?php
  205. $dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) {
  206. /* ... */
  207. }, [
  208. 'routeParser' => 'FastRoute\\RouteParser\\Std',
  209. 'dataGenerator' => 'FastRoute\\DataGenerator\\GroupCountBased',
  210. 'dispatcher' => 'FastRoute\\Dispatcher\\GroupCountBased',
  211. ]);
  212. ```
  213. The above options array corresponds to the defaults. By replacing `GroupCountBased` by
  214. `GroupPosBased` you could switch to a different dispatching strategy.
  215. ### A Note on HEAD Requests
  216. The HTTP spec requires servers to [support both GET and HEAD methods][2616-511]:
  217. > The methods GET and HEAD MUST be supported by all general-purpose servers
  218. To avoid forcing users to manually register HEAD routes for each resource we fallback to matching an
  219. available GET route for a given resource. The PHP web SAPI transparently removes the entity body
  220. from HEAD responses so this behavior has no effect on the vast majority of users.
  221. However, implementers using FastRoute outside the web SAPI environment (e.g. a custom server) MUST
  222. NOT send entity bodies generated in response to HEAD requests. If you are a non-SAPI user this is
  223. *your responsibility*; FastRoute has no purview to prevent you from breaking HTTP in such cases.
  224. Finally, note that applications MAY always specify their own HEAD method route for a given
  225. resource to bypass this behavior entirely.
  226. ### Credits
  227. This library is based on a router that [Levi Morrison][levi] implemented for the Aerys server.
  228. A large number of tests, as well as HTTP compliance considerations, were provided by [Daniel Lowrey][rdlowrey].
  229. [2616-511]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5.1.1 "RFC 2616 Section 5.1.1"
  230. [blog_post]: http://nikic.github.io/2014/02/18/Fast-request-routing-using-regular-expressions.html
  231. [levi]: https://github.com/morrisonlevi
  232. [rdlowrey]: https://github.com/rdlowrey