Skip to content

Commit 7b468f4

Browse files
move middleware docs
1 parent 5ee7664 commit 7b468f4

File tree

2 files changed

+48
-53
lines changed

2 files changed

+48
-53
lines changed

README.md

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ zenpipe(100)
4242
- [Class Methods as Operations](#class-methods-as-operations)
4343
- [Context Passing](#context-passing)
4444
- [Exception Handling](#exception-handling)
45+
- [PSR-15 Middleware](#psr-15-middleware)
4546
- [More Examples](#more-examples)
4647
5. [API Reference](#api-reference)
4748
6. [Contributing](#contributing)
4849
7. [License](#license)
49-
8. [Roadmap](#roadmap)
5050

5151
## Requirements
5252

@@ -239,6 +239,52 @@ The catch handler receives:
239239

240240
If no catch handler is set, exceptions propagate normally.
241241

242+
### PSR-15 Middleware
243+
244+
ZenPipe provides bidirectional PSR-15 middleware support. Requires `psr/http-server-middleware`.
245+
246+
#### Using PSR-15 Middleware in a Pipeline
247+
248+
Pass any `MiddlewareInterface` directly to `pipe()`:
249+
250+
```php
251+
$response = zenpipe($request)
252+
->pipe(new CorsMiddleware())
253+
->pipe(new AuthMiddleware())
254+
->pipe(fn($req, $next, $return) => $return(new Response(200)))
255+
->process();
256+
```
257+
258+
When using PSR-15 middleware, the pipeline must return a `ResponseInterface`.
259+
260+
#### Using ZenPipe as PSR-15 Middleware
261+
262+
Wrap a pipeline with `asMiddleware()` for use in PSR-15 frameworks:
263+
264+
```php
265+
$pipeline = zenpipe()
266+
->pipe(fn($req, $next) => $next($req->withAttribute('processed', true)));
267+
268+
$app->middleware($pipeline->asMiddleware());
269+
```
270+
271+
**Behavior:**
272+
- If the pipeline returns a `ResponseInterface`, it's returned directly
273+
- If the pipeline returns a `ServerRequestInterface`, it's passed to the next handler
274+
- The PSR-15 handler is available via `$context->handler` for explicit delegation
275+
276+
```php
277+
$authPipeline = zenpipe()
278+
->pipe(function ($req, $next, $return, $ctx) {
279+
if (!$req->hasHeader('Authorization')) {
280+
return $return(new Response(401));
281+
}
282+
return $ctx->handler->handle($req);
283+
});
284+
285+
$app->middleware($authPipeline->asMiddleware());
286+
```
287+
242288
### More Examples
243289

244290
#### RAG Processes
@@ -325,5 +371,3 @@ See [CONTRIBUTING.md](CONTRIBUTING.md) for details.
325371
## License
326372

327373
The MIT License (MIT). See [LICENSE](LICENSE) for details.
328-
329-
## Roadmap

docs/API.md

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -135,53 +135,4 @@ Wraps the pipeline as a PSR-15 middleware.
135135

136136
- **Returns:** A `MiddlewareInterface` instance.
137137

138-
See [PSR-15 Middleware](#psr-15-middleware) for details.
139-
140-
---
141-
142-
## PSR-15 Middleware
143-
144-
ZenPipe provides bidirectional PSR-15 middleware support. Requires `psr/http-server-middleware`.
145-
146-
### Using PSR-15 Middleware in a Pipeline
147-
148-
Pass any `MiddlewareInterface` directly to `pipe()` - it's auto-detected:
149-
150-
```php
151-
$response = zenpipe($request)
152-
->pipe(new CorsMiddleware())
153-
->pipe(new AuthMiddleware())
154-
->pipe(fn($req, $next, $return) => $return(new Response(200)))
155-
->process();
156-
```
157-
158-
When using PSR-15 middleware, the pipeline must return a `ResponseInterface`.
159-
160-
### Using ZenPipe as PSR-15 Middleware
161-
162-
Wrap a pipeline with `asMiddleware()` for use in PSR-15 frameworks:
163-
164-
```php
165-
$pipeline = zenpipe()
166-
->pipe(fn($req, $next) => $next($req->withAttribute('processed', true)));
167-
168-
$app->middleware($pipeline->asMiddleware());
169-
```
170-
171-
**Behavior:**
172-
- If the pipeline returns a `ResponseInterface`, it's returned directly
173-
- If the pipeline returns a `ServerRequestInterface`, it's passed to the next handler
174-
- The PSR-15 handler is available via `$context->handler` for explicit delegation
175-
176-
```php
177-
$authPipeline = zenpipe()
178-
->pipe(function ($req, $next, $return, $ctx) {
179-
if (!$req->hasHeader('Authorization')) {
180-
return $return(new Response(401));
181-
}
182-
// Delegate to next PSR-15 handler
183-
return $ctx->handler->handle($req);
184-
});
185-
186-
$app->middleware($authPipeline->asMiddleware());
187-
```
138+
See [PSR-15 Middleware](../README.md#psr-15-middleware) in the README for usage details.

0 commit comments

Comments
 (0)