Esc
API Index — Namespaces, Packages, Reports, Indices
ScopedMiddleware
in package
implements
MiddlewareInterface
FinalYes
Scoped Middleware — apply another middleware only to matching request paths.
Apache scopes directives with containers; this is the middleware equivalent. Wrap any middleware so it runs only when the request path matches:
ScopedMiddleware::location($inner, '/admin')—<Location "/admin">: a literal URL-path prefix (matches/admin,/admin/x, and — like Apache —/administrator; use a trailing slash or a regex for segment precision).ScopedMiddleware::match($inner, '#^/api/#')—<LocationMatch>/<FilesMatch>: a PCRE pattern against the path.
Outside the scope the inner middleware is skipped entirely and the request passes straight through to the rest of the stack. Inside the scope the inner middleware runs normally — free to short-circuit (e.g. a 403/redirect) or continue via the handler.
Usage in app.php:
$app->addMiddleware(ScopedMiddleware::location(new BasicAuthMiddleware(...), '/admin'));
$app->addMiddleware(ScopedMiddleware::match(new BlockPhpExtMiddleware(), '#\.php$#'));
Table of Contents
Interfaces
- MiddlewareInterface
Properties
Methods
- __construct() : mixed
- location() : self
<Location prefix>— literal URL-path prefix scope.- match() : self
<LocationMatch>/<FilesMatch>— PCRE path scope.- process() : ResponseInterface
Properties
$inner
private
MiddlewareInterface
$inner
$pattern
private
string
$pattern
$regex
private
bool
$regex
= false
Methods
__construct()
public
__construct(MiddlewareInterface $inner, string $pattern[, bool $regex = false ]) : mixed
Parameters
- $inner : MiddlewareInterface
- $pattern : string
- $regex : bool = false
location()
<Location prefix> — literal URL-path prefix scope.
public
static location(MiddlewareInterface $inner, string $prefix) : self
Parameters
- $inner : MiddlewareInterface
- $prefix : string
Return values
selfmatch()
<LocationMatch> / <FilesMatch> — PCRE path scope.
public
static match(MiddlewareInterface $inner, string $regex) : self
Parameters
- $inner : MiddlewareInterface
- $regex : string
Return values
selfprocess()
public
process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
Parameters
- $request : ServerRequestInterface
- $handler : RequestHandlerInterface