11# Routing
22
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-
123The base unit of routing is a ` Route ` :
134
145``` php
156namespace Zend\Router;
167
17- use Zend\Stdlib\RequestInterface as Request;
8+ use Psr\Http\Message\ServerRequestInterface as Request;
9+ use Psr\Http\Message\UriInterface;
1810
1911interface RouteInterface
2012{
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 ;
2416}
2517```
2618
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:
2921
3022``` php
3123namespace Zend\Router;
3224
33- class RouteMatch
25+ final class RouteResult
3426{
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 ;
4133}
4234```
4335
4436Typically, 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
4638their values.
4739
4840``` 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');
5052if (! $id) {
5153 throw new Exception('Required identifier is missing!');
5254}
@@ -62,10 +64,10 @@ namespace Zend\Router;
6264
6365interface RouteStackInterface extends RouteInterface
6466{
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 ;
6971}
7072```
7173
@@ -125,30 +127,15 @@ adding it to the route stack.
125127
126128### TreeRouteStack
127129
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.
133132
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 ` .
142135
143- When a route matches against a ` TreeRouteStack ` , the matched parameters from
136+ When a route matches against a ` TreeRouteStack ` , merged matched parameters from
144137each segment of the tree will be returned.
145138
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-
152139## HTTP Route Types
153140
154141zend-router ships with the following HTTP route types.
@@ -179,7 +166,7 @@ $route = Hostname::factory([
179166```
180167
181168In 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,
183170or a default value to return for the subdomain, you need to also provide
184171defaults.
185172
@@ -195,7 +182,7 @@ $route = Hostname::factory([
195182]);
196183```
197184
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"
199186and "type".
200187
201188### Zend\\ Router\\ Route\\ Literal
@@ -215,7 +202,7 @@ $route = Literal::factory([
215202```
216203
217204The 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".
219206
220207### Zend\\ Router\\ Route\\ Method
221208
@@ -234,7 +221,7 @@ $route = Method::factory([
234221```
235222
236223The 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".
238225
239226### Zend\\ Router\\ Route\\ Part
240227
@@ -328,35 +315,16 @@ You may use any route type as a child route of a `Part` route.
328315> ### Route plugins
329316>
330317> 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 `
350319>
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.
354322
355323### Zend\\ Router\\ Route\\ Regex
356324
357325A ` Regex ` route utilizes a regular expression to match against the URI path. Any
358326valid 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 ` .
360328
361329Since regular expression routes are often complex, you must specify a "spec" or
362330specification 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
365333the ` assemble() ` method.
366334
367335Just 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.
369337
370338``` php
371339$route = Regex::factory([
@@ -380,7 +348,7 @@ $route = Regex::factory([
380348```
381349
382350The 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
384352"format". When assembling a URL from this route, the "id" and "format" values
385353would be used to fill the specification.
386354
@@ -400,7 +368,7 @@ $route = Scheme::factory([
400368```
401369
402370The 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.
404372
405373### Zend\\ Router\\ Route\\ Segment
406374
0 commit comments