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.

273 lines
9.8 KiB

8 years ago
  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. ### Caching
  97. The reason `simpleDispatcher` accepts a callback for defining the routes is to allow seamless
  98. caching. By using `cachedDispatcher` instead of `simpleDispatcher` you can cache the generated
  99. routing data and construct the dispatcher from the cached information:
  100. ```php
  101. <?php
  102. $dispatcher = FastRoute\cachedDispatcher(function(FastRoute\RouteCollector $r) {
  103. $r->addRoute('GET', '/user/{name}/{id:[0-9]+}', 'handler0');
  104. $r->addRoute('GET', '/user/{id:[0-9]+}', 'handler1');
  105. $r->addRoute('GET', '/user/{name}', 'handler2');
  106. }, [
  107. 'cacheFile' => __DIR__ . '/route.cache', /* required */
  108. 'cacheDisabled' => IS_DEBUG_ENABLED, /* optional, enabled by default */
  109. ]);
  110. ```
  111. The second parameter to the function is an options array, which can be used to specify the cache
  112. file location, among other things.
  113. ### Dispatching a URI
  114. A URI is dispatched by calling the `dispatch()` method of the created dispatcher. This method
  115. accepts the HTTP method and a URI. Getting those two bits of information (and normalizing them
  116. appropriately) is your job - this library is not bound to the PHP web SAPIs.
  117. The `dispatch()` method returns an array whose first element contains a status code. It is one
  118. of `Dispatcher::NOT_FOUND`, `Dispatcher::METHOD_NOT_ALLOWED` and `Dispatcher::FOUND`. For the
  119. method not allowed status the second array element contains a list of HTTP methods allowed for
  120. the supplied URI. For example:
  121. [FastRoute\Dispatcher::METHOD_NOT_ALLOWED, ['GET', 'POST']]
  122. > **NOTE:** The HTTP specification requires that a `405 Method Not Allowed` response include the
  123. `Allow:` header to detail available methods for the requested resource. Applications using FastRoute
  124. should use the second array element to add this header when relaying a 405 response.
  125. For the found status the second array element is the handler that was associated with the route
  126. and the third array element is a dictionary of placeholder names to their values. For example:
  127. /* Routing against GET /user/nikic/42 */
  128. [FastRoute\Dispatcher::FOUND, 'handler0', ['name' => 'nikic', 'id' => '42']]
  129. ### Overriding the route parser and dispatcher
  130. The routing process makes use of three components: A route parser, a data generator and a
  131. dispatcher. The three components adhere to the following interfaces:
  132. ```php
  133. <?php
  134. namespace FastRoute;
  135. interface RouteParser {
  136. public function parse($route);
  137. }
  138. interface DataGenerator {
  139. public function addRoute($httpMethod, $routeData, $handler);
  140. public function getData();
  141. }
  142. interface Dispatcher {
  143. const NOT_FOUND = 0, FOUND = 1, METHOD_NOT_ALLOWED = 2;
  144. public function dispatch($httpMethod, $uri);
  145. }
  146. ```
  147. The route parser takes a route pattern string and converts it into an array of route infos, where
  148. each route info is again an array of it's parts. The structure is best understood using an example:
  149. /* The route /user/{id:\d+}[/{name}] converts to the following array: */
  150. [
  151. [
  152. '/user/',
  153. ['id', '\d+'],
  154. ],
  155. [
  156. '/user/',
  157. ['id', '\d+'],
  158. '/',
  159. ['name', '[^/]+'],
  160. ],
  161. ]
  162. This array can then be passed to the `addRoute()` method of a data generator. After all routes have
  163. been added the `getData()` of the generator is invoked, which returns all the routing data required
  164. by the dispatcher. The format of this data is not further specified - it is tightly coupled to
  165. the corresponding dispatcher.
  166. The dispatcher accepts the routing data via a constructor and provides a `dispatch()` method, which
  167. you're already familiar with.
  168. The route parser can be overwritten individually (to make use of some different pattern syntax),
  169. however the data generator and dispatcher should always be changed as a pair, as the output from
  170. the former is tightly coupled to the input of the latter. The reason the generator and the
  171. dispatcher are separate is that only the latter is needed when using caching (as the output of
  172. the former is what is being cached.)
  173. When using the `simpleDispatcher` / `cachedDispatcher` functions from above the override happens
  174. through the options array:
  175. ```php
  176. <?php
  177. $dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) {
  178. /* ... */
  179. }, [
  180. 'routeParser' => 'FastRoute\\RouteParser\\Std',
  181. 'dataGenerator' => 'FastRoute\\DataGenerator\\GroupCountBased',
  182. 'dispatcher' => 'FastRoute\\Dispatcher\\GroupCountBased',
  183. ]);
  184. ```
  185. The above options array corresponds to the defaults. By replacing `GroupCountBased` by
  186. `GroupPosBased` you could switch to a different dispatching strategy.
  187. ### A Note on HEAD Requests
  188. The HTTP spec requires servers to [support both GET and HEAD methods][2616-511]:
  189. > The methods GET and HEAD MUST be supported by all general-purpose servers
  190. To avoid forcing users to manually register HEAD routes for each resource we fallback to matching an
  191. available GET route for a given resource. The PHP web SAPI transparently removes the entity body
  192. from HEAD responses so this behavior has no effect on the vast majority of users.
  193. However, implementers using FastRoute outside the web SAPI environment (e.g. a custom server) MUST
  194. NOT send entity bodies generated in response to HEAD requests. If you are a non-SAPI user this is
  195. *your responsibility*; FastRoute has no purview to prevent you from breaking HTTP in such cases.
  196. Finally, note that applications MAY always specify their own HEAD method route for a given
  197. resource to bypass this behavior entirely.
  198. ### Credits
  199. This library is based on a router that [Levi Morrison][levi] implemented for the Aerys server.
  200. A large number of tests, as well as HTTP compliance considerations, were provided by [Daniel Lowrey][rdlowrey].
  201. [2616-511]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5.1.1 "RFC 2616 Section 5.1.1"
  202. [blog_post]: http://nikic.github.io/2014/02/18/Fast-request-routing-using-regular-expressions.html
  203. [levi]: https://github.com/morrisonlevi
  204. [rdlowrey]: https://github.com/rdlowrey