Alpha ZealPHP is early-stage and under active development. APIs may change between minor versions until v1.0. Feedback and bug reports welcome on GitHub.
API Index — Namespaces, Packages, Reports, Indices

CacheControlMiddleware
in package
implements MiddlewareInterface

Cache-Control Middleware

Stamps Cache-Control: max-age=N, public on responses whose URL path ends in a recognised static-asset extension. Mirrors the most common Apache .htaccess pattern for "tell the browser to cache static assets for 30 days".

Apache equivalent:

<FilesMatch "\.(css|js|jpe?g|png|gif|svg|ico|woff2?)$">
    Header set Cache-Control "max-age=2628000, public"
</FilesMatch>

nginx equivalent:

location ~* \.(css|js|jpe?g|png|gif|svg|ico|woff2?)$ {
    expires 30d;
    add_header Cache-Control "public, max-age=2628000";
}

Constructor accepts a map of extension => max-age-seconds. Defaults cover the common static-asset extensions at 30 days (2_628_000s).

Pass $publicCache = false to emit private instead of public — useful when you serve per-user assets that intermediate caches must not store.

Error-response suppression: Apache mod_expires (and the FilesMatch + Header set equivalent) never stamps caching headers on 4xx/5xx responses. Responses with status >= 400 are returned unchanged, matching Apache behaviour (mod_expires.c:455–458).

Usage in app.php:

// defaults — 30d for css/js/images/fonts
$app->addMiddleware(new \ZealPHP\Middleware\CacheControlMiddleware());

// custom: 1y for fingerprinted hashes, 5m for HTML
$app->addMiddleware(new \ZealPHP\Middleware\CacheControlMiddleware([
    'css' => 31536000, 'js' => 31536000, 'woff2' => 31536000,
    'html' => 300,
]));

Table of Contents

Interfaces

MiddlewareInterface

Constants

DEFAULT_MAP  : array<string, int> = ['css' => self::DEFAULT_MAX_AGE, 'js' => self::...
DEFAULT_MAX_AGE  : mixed = 2628000
~30 days in seconds — Apache's classic "ExpiresDefault A2628000" value.

Properties

$map  : array<string, int>
$publicCache  : bool

Methods

__construct()  : mixed
process()  : ResponseInterface
normaliseMap()  : array<string, int>

Constants

DEFAULT_MAP

private array<string, int> DEFAULT_MAP = ['css' => self::DEFAULT_MAX_AGE, 'js' => self::DEFAULT_MAX_AGE, 'mjs' => self::DEFAULT_MAX_AGE, 'jpg' => self::DEFAULT_MAX_AGE, 'jpeg' => self::DEFAULT_MAX_AGE, 'png' => self::DEFAULT_MAX_AGE, 'gif' => self::DEFAULT_MAX_AGE, 'webp' => self::DEFAULT_MAX_AGE, 'avif' => self::DEFAULT_MAX_AGE, 'svg' => self::DEFAULT_MAX_AGE, 'ico' => self::DEFAULT_MAX_AGE, 'woff' => self::DEFAULT_MAX_AGE, 'woff2' => self::DEFAULT_MAX_AGE, 'ttf' => self::DEFAULT_MAX_AGE, 'eot' => self::DEFAULT_MAX_AGE, 'otf' => self::DEFAULT_MAX_AGE, 'wasm' => self::DEFAULT_MAX_AGE]

DEFAULT_MAX_AGE

~30 days in seconds — Apache's classic "ExpiresDefault A2628000" value.

private mixed DEFAULT_MAX_AGE = 2628000

Properties

Methods

__construct()

public __construct([array<string, int>|null $map = null ][, bool $publicCache = true ]) : mixed
Parameters
$map : array<string, int>|null = null

ext => seconds; null uses defaults

$publicCache : bool = true

process()

public process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
Parameters
$request : ServerRequestInterface
$handler : RequestHandlerInterface
Return values
ResponseInterface

normaliseMap()

private normaliseMap(array<string, int> $map) : array<string, int>
Parameters
$map : array<string, int>
Return values
array<string, int>
On this page