Projekt

Obecné

Profil

Stáhnout (9.51 KB) Statistiky
| Větev: | Revize:
1
FastRoute - Fast request router for PHP
2
=======================================
3

    
4
This library provides a fast implementation of a regular expression based router. [Blog post explaining how the
5
implementation works and why it is fast.][blog_post]
6

    
7
Install
8
-------
9

    
10
To install with composer:
11

    
12
```sh
13
composer require nikic/fast-route
14
```
15

    
16
Usage
17
-----
18

    
19
Here's a basic usage example:
20

    
21
```php
22
<?php
23

    
24
require '/path/to/vendor/autoload.php';
25

    
26
$dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) {
27
    $r->addRoute('GET', '/users', 'get_all_users_handler');
28
    // {id} must be a number (\d+)
29
    $r->addRoute('GET', '/user/{id:\d+}', 'get_user_handler');
30
    // The /{title} suffix is optional
31
    $r->addRoute('GET', '/articles/{id:\d+}[/{title}]', 'get_article_handler');
32
});
33

    
34
// Fetch method and URI from somewhere
35
$httpMethod = $_SERVER['REQUEST_METHOD'];
36
$uri = rawurldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
37

    
38
$routeInfo = $dispatcher->dispatch($httpMethod, $uri);
39
switch ($routeInfo[0]) {
40
    case FastRoute\Dispatcher::NOT_FOUND:
41
        // ... 404 Not Found
42
        break;
43
    case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
44
        $allowedMethods = $routeInfo[1];
45
        // ... 405 Method Not Allowed
46
        break;
47
    case FastRoute\Dispatcher::FOUND:
48
        $handler = $routeInfo[1];
49
        $vars = $routeInfo[2];
50
        // ... call $handler with $vars
51
        break;
52
}
53
```
54

    
55
### Defining routes
56

    
57
The routes are defined by calling the `FastRoute\simpleDispatcher()` function, which accepts
58
a callable taking a `FastRoute\RouteCollector` instance. The routes are added by calling
59
`addRoute()` on the collector instance:
60

    
61
```php
62
$r->addRoute($method, $routePattern, $handler);
63
```
64

    
65
The `$method` is an uppercase HTTP method string for which a certain route should match. It
66
is possible to specify multiple valid methods using an array:
67

    
68
```php
69
// These two calls
70
$r->addRoute('GET', '/test', 'handler');
71
$r->addRoute('POST', '/test', 'handler');
72
// Are equivalent to this one call
73
$r->addRoute(['GET', 'POST'], '/test', 'handler');
74
```
75

    
76
By default the `$routePattern` uses a syntax where `{foo}` specifies a placeholder with name `foo`
77
and matching the regex `[^/]+`. To adjust the pattern the placeholder matches, you can specify
78
a custom pattern by writing `{bar:[0-9]+}`. Some examples:
79

    
80
```php
81
// Matches /user/42, but not /user/xyz
82
$r->addRoute('GET', '/user/{id:\d+}', 'handler');
83

    
84
// Matches /user/foobar, but not /user/foo/bar
85
$r->addRoute('GET', '/user/{name}', 'handler');
86

    
87
// Matches /user/foo/bar as well
88
$r->addRoute('GET', '/user/{name:.+}', 'handler');
89
```
90

    
91
Custom patterns for route placeholders cannot use capturing groups. For example `{lang:(en|de)}`
92
is not a valid placeholder, because `()` is a capturing group. Instead you can use either
93
`{lang:en|de}` or `{lang:(?:en|de)}`.
94

    
95
Furthermore parts of the route enclosed in `[...]` are considered optional, so that `/foo[bar]`
96
will match both `/foo` and `/foobar`. Optional parts are only supported in a trailing position,
97
not in the middle of a route.
98

    
99
```php
100
// This route
101
$r->addRoute('GET', '/user/{id:\d+}[/{name}]', 'handler');
102
// Is equivalent to these two routes
103
$r->addRoute('GET', '/user/{id:\d+}', 'handler');
104
$r->addRoute('GET', '/user/{id:\d+}/{name}', 'handler');
105

    
106
// This route is NOT valid, because optional parts can only occur at the end
107
$r->addRoute('GET', '/user[/{id:\d+}]/{name}', 'handler');
108
```
109

    
110
The `$handler` parameter does not necessarily have to be a callback, it could also be a controller
111
class name or any other kind of data you wish to associate with the route. FastRoute only tells you
112
which handler corresponds to your URI, how you interpret it is up to you.
113

    
114
### Caching
115

    
116
The reason `simpleDispatcher` accepts a callback for defining the routes is to allow seamless
117
caching. By using `cachedDispatcher` instead of `simpleDispatcher` you can cache the generated
118
routing data and construct the dispatcher from the cached information:
119

    
120
```php
121
<?php
122

    
123
$dispatcher = FastRoute\cachedDispatcher(function(FastRoute\RouteCollector $r) {
124
    $r->addRoute('GET', '/user/{name}/{id:[0-9]+}', 'handler0');
125
    $r->addRoute('GET', '/user/{id:[0-9]+}', 'handler1');
126
    $r->addRoute('GET', '/user/{name}', 'handler2');
127
}, [
128
    'cacheFile' => __DIR__ . '/route.cache', /* required */
129
    'cacheDisabled' => IS_DEBUG_ENABLED,     /* optional, enabled by default */
130
]);
131
```
132

    
133
The second parameter to the function is an options array, which can be used to specify the cache
134
file location, among other things.
135

    
136
### Dispatching a URI
137

    
138
A URI is dispatched by calling the `dispatch()` method of the created dispatcher. This method
139
accepts the HTTP method and a URI. Getting those two bits of information (and normalizing them
140
appropriately) is your job - this library is not bound to the PHP web SAPIs.
141

    
142
The `dispatch()` method returns an array whose first element contains a status code. It is one
143
of `Dispatcher::NOT_FOUND`, `Dispatcher::METHOD_NOT_ALLOWED` and `Dispatcher::FOUND`. For the
144
method not allowed status the second array element contains a list of HTTP methods allowed for
145
the supplied URI. For example:
146

    
147
    [FastRoute\Dispatcher::METHOD_NOT_ALLOWED, ['GET', 'POST']]
148

    
149
> **NOTE:** The HTTP specification requires that a `405 Method Not Allowed` response include the
150
`Allow:` header to detail available methods for the requested resource. Applications using FastRoute
151
should use the second array element to add this header when relaying a 405 response.
152

    
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

    
156
    /* Routing against GET /user/nikic/42 */
157

    
158
    [FastRoute\Dispatcher::FOUND, 'handler0', ['name' => 'nikic', 'id' => '42']]
159

    
160
### Overriding the route parser and dispatcher
161

    
162
The routing process makes use of three components: A route parser, a data generator and a
163
dispatcher. The three components adhere to the following interfaces:
164

    
165
```php
166
<?php
167

    
168
namespace FastRoute;
169

    
170
interface RouteParser {
171
    public function parse($route);
172
}
173

    
174
interface DataGenerator {
175
    public function addRoute($httpMethod, $routeData, $handler);
176
    public function getData();
177
}
178

    
179
interface Dispatcher {
180
    const NOT_FOUND = 0, FOUND = 1, METHOD_NOT_ALLOWED = 2;
181

    
182
    public function dispatch($httpMethod, $uri);
183
}
184
```
185

    
186
The route parser takes a route pattern string and converts it into an array of route infos, where
187
each route info is again an array of it's parts. The structure is best understood using an example:
188

    
189
    /* The route /user/{id:\d+}[/{name}] converts to the following array: */
190
    [
191
        [
192
            '/user/',
193
            ['name', '[^/]+'],
194
        ],
195
        [
196
            '/user/',
197
            ['name', '[^/]+'],
198
            '/',
199
            ['id', '[0-9]+'],
200
        ],
201
    ]
202

    
203
This array can then be passed to the `addRoute()` method of a data generator. After all routes have
204
been added the `getData()` of the generator is invoked, which returns all the routing data required
205
by the dispatcher. The format of this data is not further specified - it is tightly coupled to
206
the corresponding dispatcher.
207

    
208
The dispatcher accepts the routing data via a constructor and provides a `dispatch()` method, which
209
you're already familiar with.
210

    
211
The route parser can be overwritten individually (to make use of some different pattern syntax),
212
however the data generator and dispatcher should always be changed as a pair, as the output from
213
the former is tightly coupled to the input of the latter. The reason the generator and the
214
dispatcher are separate is that only the latter is needed when using caching (as the output of
215
the former is what is being cached.)
216

    
217
When using the `simpleDispatcher` / `cachedDispatcher` functions from above the override happens
218
through the options array:
219

    
220
```php
221
<?php
222

    
223
$dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) {
224
    /* ... */
225
}, [
226
    'routeParser' => 'FastRoute\\RouteParser\\Std',
227
    'dataGenerator' => 'FastRoute\\DataGenerator\\GroupCountBased',
228
    'dispatcher' => 'FastRoute\\Dispatcher\\GroupCountBased',
229
]);
230
```
231

    
232
The above options array corresponds to the defaults. By replacing `GroupCountBased` by
233
`GroupPosBased` you could switch to a different dispatching strategy.
234

    
235
### A Note on HEAD Requests
236

    
237
The HTTP spec requires servers to [support both GET and HEAD methods][2616-511]:
238

    
239
> The methods GET and HEAD MUST be supported by all general-purpose servers
240

    
241
To avoid forcing users to manually register HEAD routes for each resource we fallback to matching an
242
available GET route for a given resource. The PHP web SAPI transparently removes the entity body
243
from HEAD responses so this behavior has no effect on the vast majority of users.
244

    
245
However, implementers using FastRoute outside the web SAPI environment (e.g. a custom server) MUST
246
NOT send entity bodies generated in response to HEAD requests. If you are a non-SAPI user this is
247
*your responsibility*; FastRoute has no purview to prevent you from breaking HTTP in such cases.
248

    
249
Finally, note that applications MAY always specify their own HEAD method route for a given
250
resource to bypass this behavior entirely.
251

    
252
### Credits
253

    
254
This library is based on a router that [Levi Morrison][levi] implemented for the Aerys server.
255

    
256
A large number of tests, as well as HTTP compliance considerations, were provided by [Daniel Lowrey][rdlowrey].
257

    
258

    
259
[2616-511]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5.1.1 "RFC 2616 Section 5.1.1"
260
[blog_post]: http://nikic.github.io/2014/02/18/Fast-request-routing-using-regular-expressions.html
261
[levi]: https://github.com/morrisonlevi
262
[rdlowrey]: https://github.com/rdlowrey
(5-5/7)