@@ -61,20 +61,66 @@ The `ValidationExceptionMiddleware` PSR-15 middleware catches all exceptions and
6161``` php
6262<?php
6363
64+ use Selective\Validation\Encoder\JsonEncoder;
6465use Selective\Validation\Middleware\ValidationExceptionMiddleware;
6566use Slim\Factory\AppFactory;
6667
6768require_once __DIR__ . '/../vendor/autoload.php';
6869
6970$app = AppFactory::create();
7071
71- $app->add(ValidationExceptionMiddleware::class); // <--- add middleware
72+ $app->add(new ValidationExceptionMiddleware(
73+ $app->getResponseFactory(),
74+ new JsonEncoder()
75+ ));
76+
77+ // If you are using a container, you can also use this option:
78+ // $app->add(ValidationExceptionMiddleware::class);
7279
7380// ...
7481
7582$app->run();
7683```
7784
85+ ### Container definition
86+
87+ This example uses PHI-DI:
88+
89+ ``` php
90+ <?php
91+
92+ use Psr\Container\ContainerInterface;
93+ use Psr\Http\Message\ResponseFactoryInterface;
94+ use Selective\Validation\Encoder\JsonEncoder;
95+ use Selective\Validation\Middleware\ValidationExceptionMiddleware;
96+ use Slim\App;
97+ use Slim\Factory\AppFactory;
98+ // ...
99+
100+ return [
101+ ValidationExceptionMiddleware::class => static function (ContainerInterface $container) {
102+ $factory = $container->get(ResponseFactoryInterface::class);
103+
104+ return new ValidationExceptionMiddleware($factory, new JsonEncoder());
105+ },
106+
107+ ResponseFactoryInterface::class => static function (ContainerInterface $container) {
108+ $app = $container->get(App::class);
109+
110+ return $app->getResponseFactory();
111+ },
112+
113+ App::class => static function (ContainerInterface $container) {
114+ AppFactory::setContainer($container);
115+
116+ return AppFactory::create();
117+ },
118+
119+ // ...
120+
121+ ];
122+ ```
123+
78124#### Usage
79125
80126``` php
0 commit comments