Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Commit bf40d4f

Browse files
committed
Initial docs for rewrite
1 parent c8f96ef commit bf40d4f

File tree

5 files changed

+174
-80
lines changed

5 files changed

+174
-80
lines changed

docs/book/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<div class="jumbotron">
33
<h1>zend-router</h1>
44

5-
<p>Flexible routing system for HTTP and console applications.</p>
5+
<p>Flexible routing system for PSR-7 HTTP applications.</p>
66

77
<pre><code class="language-bash">$ composer require zendframework/zend-router</code></pre>
88
</div>

docs/book/intro.md

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Introduction
2+
3+
zend-router is a HTTP router implementation for
4+
[PSR-7 HTTP message interfaces](http://www.php-fig.org/psr/psr-7/).
5+
It provides dynamic routing capabilities required in most modern web
6+
applications using literal and dynamic path segments, domain, method and scheme
7+
matching.
8+
9+
## Routing
10+
11+
Routing refers to matching request URI or other request information using
12+
defined rules(routes) in order to determine an appropriate handler for the
13+
request and to extract matched values.
14+
15+
Zend-router defines `match()` method that accepts PSR-7 server request and
16+
returns `Zend\Router\RouteResult` instance to indicate the result of the
17+
matching operation.
18+
19+
`RouteResult` provides three methods to check the outcome:
20+
- `isSuccess()` will be true when route successfully matched
21+
- `isFailure()` will be true when none of the routes matched
22+
- `isMethodFailure()` will be true when some routes could match but with
23+
different HTTP method. List of allowed HTTP methods is also provided with
24+
`getAllowedMethods()`
25+
26+
## Retrieving matched parameters
27+
28+
When routing is successful, result will contain list of matched or default
29+
parameters:
30+
31+
```php
32+
$parameters = $result->getMatchedParams();
33+
```
34+
35+
Typically, application utilizing zend-router would inject matched parameters
36+
into request as *attributes*:
37+
38+
```php
39+
$id = $request->getAttribute('id');
40+
```
41+
42+
## Retrieving matched route name
43+
44+
When routing is successful, result will also contain a name of the route that
45+
matched:
46+
47+
```php
48+
$routeName = $result->getMatchedRouteName();
49+
```
50+
51+
## URI generation
52+
53+
Because router have knowledge of the various paths it can match, it is
54+
also typically used within applications to generate URIs to other application
55+
resources, avoiding the need to hardcode them.
56+
57+
You can call the `assemble()` method with a route name and a list of parameters
58+
and options to be used for URI assembling. Router will return intance of
59+
PSR-7 `UriInterface`:
60+
61+
```php
62+
$uri = $router->assemble('routename', ['id' => '123'], ['query' => ['p' => 2]]);
63+
```
64+

docs/book/migration/v3-to-v4.md

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Migrating from v3 to v4
2+
3+
zend-router v4 makes a number of significant changes that will affect your
4+
application. This document details those changes, and provides suggestions on
5+
how to update your application to work with v3.
6+
7+
## PSR-7 Rewrite
8+
9+
zend-router drops support for `Zend\Stdlib\RequestInterface` and switches to
10+
[PSR-7 HTTP message interfaces](http://www.php-fig.org/psr/psr-7/)
11+
`Psr\Http\Message\ServerRequestInterface`. That means that non-HTTP routing is
12+
no longer supported.
13+
14+
## Backward incompatible changes
15+
16+
- `Zend\Router\Http\TreeRouteStack` was moved to `Zend\Router\TreeRouteStack`
17+
- `RouteInterface::match()` method now accepts `Psr\Http\Message\ServerRequestInterface`
18+
and always returns `Zend\Router\RouteResult`.
19+
- `RouteInterface::assemble()` accepts same arguments as before, except for `'uri'` option, which
20+
must be instance of `Psr\Http\Message\UriInterface` if present.
21+
Returns instance of `Psr\Http\Message\UriInterface` or throws if route cannot
22+
be assembled.
23+
- `Zend\Router\RouteMatch` is removed and replaced with `Zend\Router\RouteResult`
24+
- `RouteInterface::match()` method is no longer allowed to return partial match.
25+
`Zend\Router\PartialRouteInterface` is introduced to provide that functionality.
26+
`Chain`, `Hostname`, `Literal`, `Method`, `Regex`, `Scheme` and `Segment`
27+
routes are now partial routes.
28+
- `Chain` route now accepts partial routes only
29+
- `Part` route now requires partial route for its base route.
30+
- Base path support was dropped from the router. It is now responsibility of an
31+
application and its url helpers.
32+
- Router no longer provides absolute URI populated from current request. It is
33+
now responsibility of application url helpers
34+
35+
## Http routes namespace change
36+
37+
In zend-router v3, HTTP specific routes were in `Zend\Router\Http` namespace.
38+
With v4 zend-router became HTTP only router so the namespace was changed to
39+
`Zend\Router\Route`.
40+
41+
If you were referring routes using their fully qualified class names in router
42+
configuration you are urged to update your code. However, old FQCN are
43+
registered as aliases in RoutePluginManager and configuration will continue
44+
work unchanged.
45+
46+
## Wildcard route removal
47+
48+
`Zend\Router\Http\Wildcard` was deprecated starting in version 2.3.0. If you
49+
were using it previously, it no longer exists. Wildcard route is considered
50+
problematic, due to the fact that arbitrary route match parameters may be
51+
specified, which could potentially lead to nefarious actions such as selection
52+
of an alternate controller or controller action.
53+
54+
55+
## Console routing
56+
57+
Console routing support was dropped from zend-router for version 4. If you use
58+
zend-mvc-console, you will need to migrate to standalone console application
59+
such as `zfcampus/zf-console` or `symfony/console`
60+

docs/book/routing.md

+46-78
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,54 @@
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-
123
The base unit of routing is a `Route`:
134

145
```php
156
namespace 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

1911
interface 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
3123
namespace 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

4436
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
4638
their 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');
5052
if (! $id) {
5153
throw new Exception('Required identifier is missing!');
5254
}
@@ -62,10 +64,10 @@ namespace Zend\Router;
6264

6365
interface 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
144137
each 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

154141
zend-router ships with the following HTTP route types.
@@ -179,7 +166,7 @@ $route = Hostname::factory([
179166
```
180167

181168
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,
183170
or a default value to return for the subdomain, you need to also provide
184171
defaults.
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"
199186
and "type".
200187

201188
### Zend\\Router\\Route\\Literal
@@ -215,7 +202,7 @@ $route = Literal::factory([
215202
```
216203

217204
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".
219206

220207
### Zend\\Router\\Route\\Method
221208

@@ -234,7 +221,7 @@ $route = Method::factory([
234221
```
235222

236223
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".
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

357325
A `Regex` route utilizes a regular expression to match against the URI path. Any
358326
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`.
360328

361329
Since regular expression routes are often complex, you must specify a "spec" or
362330
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
365333
the `assemble()` method.
366334

367335
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.
369337

370338
```php
371339
$route = Regex::factory([
@@ -380,7 +348,7 @@ $route = Regex::factory([
380348
```
381349

382350
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
384352
"format". When assembling a URL from this route, the "id" and "format" values
385353
would be used to fill the specification.
386354

@@ -400,7 +368,7 @@ $route = Scheme::factory([
400368
```
401369

402370
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.
404372

405373
### Zend\\Router\\Route\\Segment
406374

mkdocs.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ docs_dir: docs/book
22
site_dir: docs/html
33
pages:
44
- index.md
5+
- Introduction: intro.md
56
- Routing: routing.md
67
- Migration:
78
- "V2 to V3": migration/v2-to-v3.md
9+
- "V3 to V4": migration/v3-to-v4.md
810
site_name: zend-router
9-
site_description: 'zend-router: Flexible routing system for HTTP applications'
11+
site_description: 'zend-router: Flexible routing system for PSR-7 HTTP applications'
1012
repo_url: 'https://github.com/zendframework/zend-router'
1113
copyright: 'Copyright (c) 2005-2017 <a href="http://www.zend.com/">Zend Technologies USA Inc.</a>'

0 commit comments

Comments
 (0)