Esc
API Index — Namespaces, Packages, Reports, Indices
SetEnvIfMiddleware
in package
implements
MiddlewareInterface
SetEnvIf Middleware — Apache mod_setenvif parity.
Sets request "environment" variables (into $g->server, where mod_php code
reads them as $_SERVER) when an attribute of the request matches a regex.
The classic use is tagging bots, internal IPs, or URL areas so downstream
middleware / handlers can branch on a simple flag.
Attribute names mirror Apache: the special tokens Remote_Addr,
Remote_Host, Server_Addr, Request_Method, Request_Protocol,
Request_URI; any other name is treated as a request header (so
User-Agent gives BrowserMatch behavior).
Apache equivalent:
SetEnvIf User-Agent "bot" IS_BOT=1
SetEnvIf Request_URI "^/admin" ADMIN_AREA=1
BrowserMatch "MSIE" old_browser=1
Usage in app.php:
$app->addMiddleware(new \ZealPHP\Middleware\SetEnvIfMiddleware([
['attr' => 'User-Agent', 'regex' => '#bot#i', 'set' => ['IS_BOT' => '1']],
['attr' => 'Request_URI', 'regex' => '#^/admin#', 'set' => ['ADMIN_AREA' => '1']],
]));
Table of Contents
Interfaces
- MiddlewareInterface
Properties
-
$rules
: array<int, array{attr: string, regex: string, set: array
}>
Methods
- __construct() : mixed
- process() : ResponseInterface
- attribute() : string
- str() : string
Properties
$rules
private
array<int, array{attr: string, regex: string, set: array}>
$rules
= []
Methods
__construct()
public
__construct(array<int, array<string, mixed>> $rules) : mixed
Parameters
- $rules : array<int, array<string, mixed>>
-
Each rule:
attr(string),regex(PCRE string),set(map of env var => value).
process()
public
process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
Parameters
- $request : ServerRequestInterface
- $handler : RequestHandlerInterface
Return values
ResponseInterfaceattribute()
private
attribute(ServerRequestInterface $request, RequestContext $g, string $attr) : string
Parameters
- $request : ServerRequestInterface
- $g : RequestContext
- $attr : string
Return values
stringstr()
private
static str(mixed $v) : string
Parameters
- $v : mixed