1
1
# Routing
2
2
3
- Routing is the act of matching a request to a given controller.
4
-
5
- Typically, routing will examine the request URI, and attempt to match the URI
6
- path segment against provided constraints. If the constraints match, a set of
7
- "matches" are returned, one of which should be the controller name to execute.
8
- Routing can utilize other portions of the request URI or environment as well.
9
- For example, the host or scheme, query parameters, headers, request method, and
10
- more.
11
-
12
3
The base unit of routing is a ` Route ` :
13
4
14
5
``` php
15
6
namespace Zend\Router;
16
7
17
- use Zend\Stdlib\RequestInterface as Request;
8
+ use Psr\Http\Message\ServerRequestInterface as Request;
9
+ use Psr\Http\Message\UriInterface;
18
10
19
11
interface RouteInterface
20
12
{
21
- public static function factory(array $options = []);
22
- public function match(Request $request) ;
23
- public function assemble(array $params = [], array $options = []);
13
+ public static function factory($options = []) : RouteInterface ;
14
+ public function match(Request $request, int $pathOffset = 0, array $options = []) : RouteResult ;
15
+ public function assemble(array $params = [], array $options = []) : UriInterface ;
24
16
}
25
17
```
26
18
27
- A ` Route ` accepts a ` Request ` , and determines if it matches. If so, it returns a
28
- ` RouteMatch ` object:
19
+ A ` Route ` accepts a ` Request ` , and determines if it matches. It returns a
20
+ ` RouteResult ` object:
29
21
30
22
``` php
31
23
namespace Zend\Router;
32
24
33
- class RouteMatch
25
+ final class RouteResult
34
26
{
35
- public function __construct(array $params) ;
36
- public function setMatchedRouteName($name) ;
37
- public function getMatchedRouteName() ;
38
- public function setParam($name, $value) ;
39
- public function getParams() ;
40
- public function getParam($name, $default = null) ;
27
+ public function isSuccess() : bool ;
28
+ public function isFailure() : bool ;
29
+ public function isMethodFailure() : bool ;
30
+ public function getMatchedRouteName() : ?string ;
31
+ public function getMatchedParams() : array ;
32
+ public function getAllowedMethods() : ?array ;
41
33
}
42
34
```
43
35
44
36
Typically, when a ` Route ` matches, it will define one or more parameters. These
45
- are passed into the ` RouteMatch ` , and objects may query the ` RouteMatch ` for
37
+ are passed into the ` RouteResult ` , and objects may query the ` RouteResult ` for
46
38
their values.
47
39
48
40
``` php
49
- $id = $routeMatch->getParam('id', false);
41
+ $id = $routeResult->getMatchedParams()['id'] ?? null;
42
+ if (! $id) {
43
+ throw new Exception('Required identifier is missing!');
44
+ }
45
+ $entity = $resource->get($id);
46
+ ```
47
+
48
+ Typically, application will inject matched parameters into request as attributes.
49
+
50
+ ``` php
51
+ $id = $request->getAttribute('id');
50
52
if (! $id) {
51
53
throw new Exception('Required identifier is missing!');
52
54
}
@@ -62,10 +64,10 @@ namespace Zend\Router;
62
64
63
65
interface RouteStackInterface extends RouteInterface
64
66
{
65
- public function addRoute($name, $route, $priority = null);
66
- public function addRoutes(array $routes);
67
- public function removeRoute($name);
68
- public function setRoutes(array $routes);
67
+ public function addRoute($name, $route, $priority = null) : void ;
68
+ public function addRoutes(array $routes) : void ;
69
+ public function removeRoute($name) : void ;
70
+ public function setRoutes(array $routes) : void ;
69
71
}
70
72
```
71
73
@@ -125,30 +127,15 @@ adding it to the route stack.
125
127
126
128
### TreeRouteStack
127
129
128
- ` Zend\Router\TreeRouteStack ` provides the ability to register trees of
129
- routes, and uses a B-tree algorithm to match routes. As such, you register a
130
- single route with many children.
131
-
132
- A ` TreeRouteStack ` will consist of the following configuration:
130
+ ` Zend\Router\TreeRouteStack ` is similar to ` SimpleRouteStack ` but provides the
131
+ ability to register trees of routes.
133
132
134
- - A base "route", which describes the base match needed, the root of the tree.
135
- - An optional ` route_plugins ` , which is a configured
136
- ` Zend\Router\RoutePluginManager ` that can lazy-load routes.
137
- - The option ` may_terminate ` , which hints to the router that no other segments
138
- will follow it.
139
- - An optional ` child_routes ` array, which contains additional routes that stem
140
- from the base "route" (i.e., build from it). Each child route can itself be a
141
- ` TreeRouteStack ` if desired; in fact, the ` Part ` route works exactly this way.
133
+ Route name in ` TreeRouteStack ` route tree is identified by the individual route names
134
+ joined with ` / ` as a separarator so that it looks like ` parent/child ` .
142
135
143
- When a route matches against a ` TreeRouteStack ` , the matched parameters from
136
+ When a route matches against a ` TreeRouteStack ` , merged matched parameters from
144
137
each segment of the tree will be returned.
145
138
146
- A ` TreeRouteStack ` can be your sole route for your application, or describe
147
- particular path segments of the application.
148
-
149
- An example of a ` TreeRouteStack ` is provided in the documentation of the ` Part `
150
- route.
151
-
152
139
## HTTP Route Types
153
140
154
141
zend-router ships with the following HTTP route types.
@@ -179,7 +166,7 @@ $route = Hostname::factory([
179
166
```
180
167
181
168
In the above example, only a "subdomain" key will be returned in the
182
- ` RouteMatch ` . If you wanted to also provide other information based on matching,
169
+ ` RouteResult ` . If you wanted to also provide other information based on matching,
183
170
or a default value to return for the subdomain, you need to also provide
184
171
defaults.
185
172
@@ -195,7 +182,7 @@ $route = Hostname::factory([
195
182
]);
196
183
```
197
184
198
- When matched, the above will return two keys in the ` RouteMatch ` , "subdomain"
185
+ When matched, the above will return two keys in the ` RouteResult ` , "subdomain"
199
186
and "type".
200
187
201
188
### Zend\\ Router\\ Route\\ Literal
@@ -215,7 +202,7 @@ $route = Literal::factory([
215
202
```
216
203
217
204
The above route would match a path "/foo", and return the key "action" in the
218
- ` RouteMatch ` , with the value "foo".
205
+ ` RouteResult ` , with the value "foo".
219
206
220
207
### Zend\\ Router\\ Route\\ Method
221
208
@@ -234,7 +221,7 @@ $route = Method::factory([
234
221
```
235
222
236
223
The above route would match an http "POST" or "PUT" request and return a
237
- ` RouteMatch ` object containing a key "action" with a value of "form-submit".
224
+ ` RouteResult ` object containing a key "action" with a value of "form-submit".
238
225
239
226
### Zend\\ Router\\ Route\\ Part
240
227
@@ -328,35 +315,16 @@ You may use any route type as a child route of a `Part` route.
328
315
> ### Route plugins
329
316
>
330
317
> In the above example, the ` $routePlugins ` is an instance of
331
- > ` Zend\Router\RoutePluginManager ` , containing essentially the following
332
- > configuration:
333
- >
334
- > ``` php
335
- > $routePlugins = new Zend\Router\RoutePluginManager();
336
- > $plugins = [
337
- > 'hostname' => 'Zend\Router\Route\Hostname',
338
- > 'literal' => 'Zend\Router\Route\Literal',
339
- > 'part' => 'Zend\Router\Route\Part',
340
- > 'regex' => 'Zend\Router\Route\Regex',
341
- > 'scheme' => 'Zend\Router\Route\Scheme',
342
- > 'segment' => 'Zend\Router\Route\Segment',
343
- > 'wildcard' => 'Zend\Router\Route\Wildcard',
344
- > 'method' => 'Zend\Router\Route\Method',
345
- > ];
346
- > foreach ($plugins as $name => $class) {
347
- > $routePlugins->setInvokableClass($name, $class);
348
- > }
349
- > ```
318
+ > ` Zend\Router\RoutePluginManager `
350
319
>
351
- > When using `Zend\Router\TreeRouteStack`, the `RoutePluginManager` is
352
- > set up by default, and the developer does not need to worry about autoloading
353
- > of standard HTTP routes.
320
+ > ` RoutePluginManager ` is already configured with default routes, and the
321
+ > developer does not need to worry about registering standard HTTP routes.
354
322
355
323
### Zend\\ Router\\ Route\\ Regex
356
324
357
325
A ` Regex ` route utilizes a regular expression to match against the URI path. Any
358
326
valid regular expression is allowed; our recommendation is to use named captures
359
- for any values you want to return in the `RouteMatch `.
327
+ for any values you want to return in the ` RouteResult ` .
360
328
361
329
Since regular expression routes are often complex, you must specify a "spec" or
362
330
specification to use when assembling URLs from regex routes. The spec is simply
@@ -365,7 +333,7 @@ the keys coming from either the captured values or named parameters passed to
365
333
the ` assemble() ` method.
366
334
367
335
Just like other routes, the ` Regex ` route can accept "defaults", parameters to
368
- include in the `RouteMatch ` when successfully matched.
336
+ include in the ` RouteResult ` when successfully matched.
369
337
370
338
``` php
371
339
$route = Regex::factory([
@@ -380,7 +348,7 @@ $route = Regex::factory([
380
348
```
381
349
382
350
The above would match ` /blog/001-some-blog_slug-here.html ` , and return four
383
- items in the ` RouteMatch ` , an "id", the "controller", the "action", and the
351
+ items in the ` RouteResult ` , an "id", the "controller", the "action", and the
384
352
"format". When assembling a URL from this route, the "id" and "format" values
385
353
would be used to fill the specification.
386
354
@@ -400,7 +368,7 @@ $route = Scheme::factory([
400
368
```
401
369
402
370
The above route would match the "https" scheme, and return the key "https" in
403
- the ` RouteMatch ` with a boolean ` true ` value.
371
+ the ` RouteResult ` with a boolean ` true ` value.
404
372
405
373
### Zend\\ Router\\ Route\\ Segment
406
374
0 commit comments