Skip to content

Commit 3d3216b

Browse files
committed
feat: document response processors
1 parent 95c87ce commit 3d3216b

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/Web/Documentation/content/main/1-essentials/02-controllers.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,28 @@ final readonly class JsonController
446446
}
447447
```
448448

449+
### Post-processing responses
450+
451+
There are some situations in which you may need to act on a response right before it is sent to the client. For instance, you may want to display custom error error pages when an exception occurred, or redirect somewhere instead of displaying the [built-in HTTP 404](/hello-from-the-void) page.
452+
453+
This may be done using a response processor. Similar to [view processors](./03-views#pre-processing-views), they are classes that implement the {`Tempest\Response\ResponseProcessor`} interface. In the `process()` method, you may mutate and return the response object:
454+
455+
```php src/ErrorResponseProcessor.php
456+
use function Tempest\view;
457+
458+
final class ErrorResponseProcessor implements ResponseProcessor
459+
{
460+
public function process(Response $response): Response
461+
{
462+
if (! $response->status->isSuccessful()) {
463+
return $response->setBody(view('./error.view.php', status: $response->status));
464+
}
465+
466+
return $response;
467+
}
468+
}
469+
```
470+
449471
## Custom route attributes
450472

451473
It is often a requirement to have a bunch of routes following the same specifications—for instance, using the same middleware, or the same URI prefix.

src/Web/Documentation/content/main/1-essentials/03-views.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -453,11 +453,11 @@ Optionally, it accepts an `entrypoint` attribute. If it is passed, the component
453453
<x-vite-tags entrypoint="src/main.ts" />
454454
```
455455

456-
## View processors
456+
## Pre-processing views
457457

458-
In most applications, a lot of views will need access to common data. To avoid having to manually provide this data to views through controller methods, it is possible to use view processors to manipulate views before they are rendered.
458+
In most applications, some views will need access to common data. To avoid having to manually provide this data to views through controller methods, it is possible to use view processors to manipulate views before they are rendered.
459459

460-
To create a view processor, implement the {`Tempest\View\ViewProcessor`} interface on a class. This interface requires a `process` method, which accepts and returns the view that will be rendered.
460+
To create a view processor, create a class that implements the {`Tempest\View\ViewProcessor`} interface. It requires a `process()` method in which you may mutate and return the view that will be rendered.
461461

462462
```php
463463
use Tempest\View\View;

0 commit comments

Comments
 (0)