API Index — Namespaces, Packages, Reports, Indices
BodyRewriteMiddleware
in package
implements
MiddlewareInterface
Body Rewrite Middleware (mod_substitute equivalent)
Applies an ordered list of regex substitutions to text-ish response bodies.
Useful for upstream URL rewriting (HTTP -> HTTPS in legacy app HTML),
dropping internal hostnames from rendered output, or stamping a build hash
onto every page without touching the templates.
Apache equivalent (mod_substitute):
AddOutputFilterByType SUBSTITUTE text/html
Substitute "s|http://internal.lan|https://public.example.com|n"
Only fires on text-ish content types (text/*, application/json,
application/xml, application/javascript, …). Streaming responses are
skipped — the body isn't materialised at this layer.
Each rule is ['pattern' => $regex, 'replacement' => $replacement]. The
pattern must include delimiters and any modifier flags. Replacement
follows PHP preg_replace() syntax — $1, $2, … for capture groups.
Usage in app.php:
$app->addMiddleware(new \ZealPHP\Middleware\BodyRewriteMiddleware([
['pattern' => '#http://internal\.lan#', 'replacement' => 'https://example.com'],
['pattern' => '/Powered by Old/i', 'replacement' => 'Powered by ZealPHP'],
]));
Table of Contents
Interfaces
- MiddlewareInterface
Constants
- TEXTISH_PREFIXES : mixed = ['text/', 'application/json', 'application/xml'...
Properties
- $rules : array<int, array{pattern: string, replacement: string}>
Methods
- __construct() : mixed
- process() : ResponseInterface
- isTextish() : bool
Constants
TEXTISH_PREFIXES
private
mixed
TEXTISH_PREFIXES
= ['text/', 'application/json', 'application/xml', 'application/javascript', 'application/xhtml+xml', 'image/svg+xml']
Properties
$rules
private
array<int, array{pattern: string, replacement: string}>
$rules
Methods
__construct()
public
__construct([array<int, array{pattern: string, replacement: string}> $rules = [] ]) : mixed
Parameters
- $rules : array<int, array{pattern: string, replacement: string}> = []
process()
public
process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
Parameters
- $request : ServerRequestInterface
- $handler : RequestHandlerInterface
Return values
ResponseInterfaceisTextish()
private
isTextish(string $ct) : bool
Parameters
- $ct : string