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
The package is a [PSR-15](https://www.php-fig.org/psr/psr-15/) middleware dispatcher.
18
+
The package is a [PSR-15](https://www.php-fig.org/psr/psr-15/) middleware dispatcher. Given a set of middleware and a
19
+
request instance, dispatcher executes it producing a response instance.
use Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher;
39
+
use Yiisoft\Middleware\Dispatcher\MiddlewareFactory;
40
+
use Yiisoft\Middleware\Dispatcher\MiddlewareStack;
41
+
42
+
$dispatcher = new MiddlewareDispatcher(
43
+
new MiddlewareFactory($diContainer),
44
+
new MiddlewareStack($eventDispatcher)
45
+
);
46
+
```
47
+
48
+
In the above `$diContainer` is an instance of [PSR-11](https://www.php-fig.org/psr/psr-11/)`\Psr\Container\ContainerInterface`
49
+
and `$eventDispatcher` is an instance of [PSR-14](https://www.php-fig.org/psr/psr-14/)`Psr\EventDispatcher\EventDispatcherInterface`.
50
+
51
+
After dispatcher instance obtained, it should be fed with some middleware:
52
+
53
+
```php
54
+
$dispatcher = $dispatcher->withMiddlewares([
55
+
static function (): ResponseInterface {
56
+
return new Response(418);
57
+
},
58
+
]);
59
+
```
60
+
61
+
In the above we have used a callback. Overall the following options are available:
62
+
63
+
- A controller handler action in format `[TestController::class, 'index']`. `TestController` instance will be created and
64
+
`index()` method will be executed.
65
+
- A name of PSR-15 middleware class. The middleware instance will be obtained from container executed.
66
+
- A function returning a middleware such as
67
+
```php
68
+
static function (): MiddlewareInterface {
69
+
return new TestMiddleware();
70
+
}
71
+
```
72
+
The middleware returned will be executed.
73
+
- A callback `function(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface`.
74
+
75
+
For handler action and callable typed parameters are automatically injected using dependency injection container.
76
+
Current request and handler could be obtained by type-hinting for `ServerRequestInterface` and `RequestHandlerInterface`.
77
+
78
+
After middleware set is defined, you can do the dispatching:
0 commit comments