Middleware implements client-side caching by using standard headers like Cache-Control, ETag, and Last-Modified.
It optimizes responses by validating cache freshness and optionally returning 304 Not Modified responses when content
has not changed.
The middleware only handles GET and HEAD HTTP methods, other methods are passed through unmodified.
It uses providers to get cache-related metadata for each request:
Cache-Controlheader value provider (CacheControlProviderInterface);Last-Modifieddate provider (LastModifiedProviderInterface);ETagprovider (ETagProviderInterface).
If the incoming request contains conditional headers (If-None-Match or If-Modified-Since), the middleware validates
the cache and may return a 304 Not Modified response without a message body.
ETag, Cache-Control, and Last-Modified headers are also added to the response.
Related links:
use Psr\Http\Message\ResponseFactoryInterface;
use Yiisoft\HttpMiddleware\HttpCache\CacheControlProvider\CacheControlProviderInterface;
use Yiisoft\HttpMiddleware\HttpCache\ETagProvider\ETagProviderInterface;
use Yiisoft\HttpMiddleware\HttpCache\HttpCacheMiddleware;
use Yiisoft\HttpMiddleware\HttpCache\LastModifiedProvider\LastModifiedProviderInterface;
/**
* @var ResponseFactoryInterface $responseFactory
* @var CacheControlProviderInterface $cacheControlProvider
* @var LastModifiedProviderInterface $lastModifiedProvider
* @var ETagProviderInterface $eTagProvider
*/
$middleware = new HttpCacheMiddleware(
$responseFactory,
$cacheControlProvider,
$lastModifiedProvider,
$eTagProvider,
);Type: Psr\Http\Message\ResponseFactoryInterface
A PSR-17 response factory used to create redirect responses.
Type: Yiisoft\HttpMiddleware\HttpCache\CacheControlProvider\CacheControlProviderInterface
Default: new NullCacheControlProvider()
An instance of CacheControlProviderInterface that provides the value for the Cache-Control header.
Type: Yiisoft\HttpMiddleware\HttpCache\LastModifiedProvider\LastModifiedProviderInterface
Default: new NullLastModifiedProvider()
An instance of LastModifiedProviderInterface that provides the last modified date for the given server request.
Type: Yiisoft\HttpMiddleware\HttpCache\ETagProvider\ETagProviderInterface
Default: new NullETagProvider()
An instance of ETagProviderInterface that provides the ETag instance with ETag metadata.
Type: Yiisoft\HttpMiddleware\HttpCache\ETagGenerator\ETagGeneratorInterface
Default: new DefaultETagGenerator()
An instance of ETagGeneratorInterface that generates a string ETag value based on the provided seed.
A provider should implement the CacheControlProviderInterface interface to supply the value of the Cache-Control
header based on the incoming request.
Implementations out of the box:
NullCacheControlProvider— always returnsnull, it disables cache control.ConstantCacheControlProvider— returns a predefined static cache control header value regardless of the request. Default ispublic, max-age=3600(value of constantConstantCacheControlProvider::DEFAULT_VALUE).
A provider should implement the LastModifiedProviderInterface interface to supply the last modified date for the given
server request.
Implementations out of the box:
NullLastModifiedProvider— always returnsnull, it disables the last modified date.PredefinedLastModifiedProvider— provides a sequence of predefinedDateTimeImmutablevalues as last modified dates. It is mainly intended for testing purposes. The provider returns dates one by one for each request and throws an exception when no more dates are available.
A provider should implement the ETagProviderInterface interface to supply the ETag instance with ETag metadata.
ETag metadata includes seed, which is used to generate the ETag value, and the weak flag that indicates whether the
ETag is weak or strong. A weak ETag indicates that the content has not changed semantically, while a strong ETag
indicates that the content has not changed at all.
Implementations out of the box:
NullETagProvider— always returnsnull, it disables theETagheader.PredefinedETagProvider— provides a sequence of predefinedETaginstances. It is mainly intended for testing purposes. The provider returnsETagvalues one by one for each request and throws an exception when no more values are available.
An ETagGeneratorInterface implementation is used to generate a string ETag value based on the provided seed.
Implementations out of the box:
DefaultETagGenerator— generates a stringETagvalue based on the provided seed using PHP native functionsbase64_encode()andsha1().CallableETagGenerator— allows you to pass a callable that generates theETagvalue based on the provided seed.