You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This package provides a routing `RequestHandler` for [Amp's HTTP server](https://github.com/amphp/http-server) based on the request URI and method based on[FastRoute](https://github.com/nikic/FastRoute).
3
+
This package provides a routing `RequestHandler` for [Amp's HTTP server](https://github.com/amphp/http-server) based on the request URI and method using[FastRoute](https://github.com/nikic/FastRoute).
**`Router`** implements `RequestHandler`. Any attached `RequestHandler` and `Middleware` instances will receive any `ServerObserver` events.
15
+
**`Router`** implements `RequestHandler`, which is used by an [`HttpServer`](https://github.com/amphp/http-server#creating-an-http-server) to handle incoming requests. Incoming requests are routed by `Router` to other `RequestHandler`s based on the request path.
16
16
17
-
Routes can be defined using the `addRoute($method, $uri, $requestHandler)` method. Please read the [FastRoute documentation on how to define placeholders](https://github.com/nikic/FastRoute#defining-routes).
17
+
Routes can be defined using the `addRoute($method, $uri, $requestHandler, ...$middeware)` method.
18
+
19
+
```php
20
+
public function addRoute(
21
+
string $method,
22
+
string $uri,
23
+
RequestHandler $requestHandler,
24
+
Middleware ...$middlewares,
25
+
): void
26
+
```
18
27
19
28
Matched route arguments are available in the request attributes under the `Amp\Http\Server\Router` key as an associative array.
20
29
30
+
Middleware provided to `addRoute()` will only be applied to the given route.
31
+
32
+
Please read the [FastRoute documentation on how to define placeholders](https://github.com/nikic/FastRoute#defining-routes).
33
+
34
+
### Middleware
35
+
36
+
In addition to specifying middlewares by route with `addRoute()`, you may wrap all routes with a common set of middlware using `stackMiddleware(...$middleware)`.
37
+
38
+
```php
39
+
public function stackMiddleware(Middleware ...$middlewares): void
40
+
```
41
+
42
+
### Fallback
43
+
44
+
If no routes match a request path, you can specify another instance of `RequestHandler` which will handle any unmatched routes. If no fallback handler is provided, a 404 response will be returned using the instance of `ErrorHandler` provided to the `Router` constructor.
45
+
46
+
```php
47
+
public function setFallback(RequestHandler $requestHandler): void
48
+
```
49
+
21
50
## Example
22
51
23
52
```php
24
-
$router = new Router;
53
+
use Amp\Http\HttpStatus;
54
+
use Amp\Http\Server\DefaultErrorHandler;
55
+
use Amp\Http\Server\Request;
56
+
use Amp\Http\Server\RequestHandler\ClosureRequestHandler;
57
+
use Amp\Http\Server\Response;
58
+
use Amp\Http\Server\Router;
59
+
use Amp\Http\Server\SocketHttpServer;
25
60
26
-
$router->addRoute('GET', '/', new CallableRequestHandler(function () {
27
-
return new Response(HttpStatus::OK, ['content-type' => 'text/plain'], 'Hello, world!');
0 commit comments