Esc
API Index — Namespaces, Packages, Reports, Indices
RedirectMiddleware
in package
implements
MiddlewareInterface
Redirect Middleware — declarative URL redirects, Apache mod_alias parity.
Two rule shapes, matched in order; the first match short-circuits with a redirect response (the route handler never runs):
- Prefix (Apache
Redirect /old /new): matches the path exactly or as a/old/...prefix; the remainder after the prefix is appended to the target —/old/x→/new/x. - Regex (Apache
RedirectMatch ^/old/(.*) /new/$1): PCRE pattern with$nbackreferences in the target.
The request query string is preserved (appended to the target when the target doesn't carry its own), matching mod_alias.
Usage in app.php:
$app->addMiddleware(new \ZealPHP\Middleware\RedirectMiddleware([
['from' => '/old', 'to' => '/new', 'status' => 301],
['match' => '#^/blog/(\d+)$#', 'to' => '/posts/$1', 'status' => 302],
]));
Table of Contents
Interfaces
- MiddlewareInterface
Properties
- $rules : array<int, array{prefix: ?string, regex: ?string, to: string, status: int}>
Methods
- __construct() : mixed
- process() : ResponseInterface
- resolve() : string|null
Properties
$rules
private
array<int, array{prefix: ?string, regex: ?string, to: string, status: int}>
$rules
= []
Methods
__construct()
public
__construct(array<int, array<string, mixed>> $rules) : mixed
Parameters
- $rules : array<int, array<string, mixed>>
-
Each rule needs a
toplus eitherfrom(prefix) ormatch(regex); optionalstatus(default 302).
process()
public
process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
Parameters
- $request : ServerRequestInterface
- $handler : RequestHandlerInterface
Return values
ResponseInterfaceresolve()
private
resolve(array{prefix: ?string, regex: ?string, to: string, status: int} $rule, string $path) : string|null
Parameters
- $rule : array{prefix: ?string, regex: ?string, to: string, status: int}
- $path : string