0 follower

Final Class Yiisoft\Yii\Middleware\HttpCache

InheritanceYiisoft\Yii\Middleware\HttpCache
ImplementsPsr\Http\Server\MiddlewareInterface
Deprecated since version Use `HttpCacheMiddleware` from `yiisoft/http-middleware` package instead.

HttpCache implements client-side caching by utilizing the Last-Modified and ETag HTTP headers.

Public Methods

Hide inherited methods

Method Description Defined By
process() Yiisoft\Yii\Middleware\HttpCache
withCacheControlHeader() Returns a new instance with the specified value of the Cache-Control HTTP header. Yiisoft\Yii\Middleware\HttpCache
withEtagSeed() Returns a new instance with the specified callable that generates the ETag seed string. Yiisoft\Yii\Middleware\HttpCache
withLastModified() Returns a new instance with the specified callable that generates the last modified UNIX timestamp. Yiisoft\Yii\Middleware\HttpCache
withParams() Returns a new instance with the specified extra parameters for ETag seed string generation. Yiisoft\Yii\Middleware\HttpCache
withWeakEtag() Returns a new instance with weak ETags generation enabled. Disabled by default. Yiisoft\Yii\Middleware\HttpCache

Method Details

Hide inherited methods

process() public method

public \Psr\Http\Message\ResponseInterface process ( \Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Server\RequestHandlerInterface $handler )
$request \Psr\Http\Message\ServerRequestInterface
$handler \Psr\Http\Server\RequestHandlerInterface

                public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
    if (
        ($this->lastModified === null && $this->etagSeed === null) ||
        !in_array($request->getMethod(), [Method::GET, Method::HEAD], true)
    ) {
        return $handler->handle($request);
    }
    /** @var int|null $lastModified */
    $lastModified = $this->lastModified === null ? null : ($this->lastModified)($request, $this->params);
    $etag = null;
    if ($this->etagSeed !== null) {
        /** @var string|null $seed */
        $seed = ($this->etagSeed)($request, $this->params);
        if ($seed !== null) {
            $etag = $this->generateEtag($seed);
        }
    }
    $response = $handler->handle($request);
    if ($this->cacheControlHeader !== null) {
        $response = $response->withHeader(Header::CACHE_CONTROL, $this->cacheControlHeader);
    }
    if ($etag !== null) {
        $response = $response->withHeader(Header::ETAG, $etag);
    }
    $cacheIsValid = $this->validateCache($request, $lastModified, $etag);
    if ($cacheIsValid) {
        $response = $response->withStatus(Status::NOT_MODIFIED);
    }
    /** @see https://tools.ietf.org/html/rfc7232#section-4.1 */
    if ($lastModified !== null && (!$cacheIsValid || $etag === null)) {
        $response = $response->withHeader(
            Header::LAST_MODIFIED,
            gmdate('D, d M Y H:i:s', $lastModified) . ' GMT',
        );
    }
    return $response;
}

            
withCacheControlHeader() public method

Returns a new instance with the specified value of the Cache-Control HTTP header.

See also https://tools.ietf.org/html/rfc2616#section-14.9.

public self withCacheControlHeader ( string|null $header )
$header string|null

The value of the Cache-Control HTTP header. If null, the header won't be sent.

                public function withCacheControlHeader(?string $header): self
{
    $new = clone $this;
    $new->cacheControlHeader = $header;
    return $new;
}

            
withEtagSeed() public method

Returns a new instance with the specified callable that generates the ETag seed string.

public self withEtagSeed ( callable $etagSeed )
$etagSeed callable

A PHP callback that generates the ETag seed string.

The callback's signature should be:

function (ServerRequestInterface $request, mixed $params): string;

Where $request is the {@see \Psr\Http\Message\ServerRequestInterface} object that this middleware is currently handling; $params takes the value of {@see \Yiisoft\Yii\Middleware\withParams()}. The callback should return a string serving as the seed for generating an ETag.

                public function withEtagSeed(callable $etagSeed): self
{
    $new = clone $this;
    $new->etagSeed = $etagSeed;
    return $new;
}

            
withLastModified() public method

Returns a new instance with the specified callable that generates the last modified UNIX timestamp.

See also https://tools.ietf.org/html/rfc7232#section-2.2.

public self withLastModified ( callable $lastModified )
$lastModified callable

A PHP callback that returns the UNIX timestamp of the last modification time.

The callback's signature should be:

function (ServerRequestInterface $request, mixed $params): int;

Where $request is the {@see \Psr\Http\Message\ServerRequestInterface} object that this filter is currently handling; $params takes the value of {@see \Yiisoft\Yii\Middleware\withParams()}. The callback should return a UNIX timestamp.

                public function withLastModified(callable $lastModified): self
{
    $new = clone $this;
    $new->lastModified = $lastModified;
    return $new;
}

            
withParams() public method

Returns a new instance with the specified extra parameters for ETag seed string generation.

public self withParams ( mixed $params )
$params mixed

Extra parameters for {@see \Yiisoft\Yii\Middleware\withEtagSeed()} callbacks.

                public function withParams(mixed $params): self
{
    $new = clone $this;
    $new->params = $params;
    return $new;
}

            
withWeakEtag() public method

Returns a new instance with weak ETags generation enabled. Disabled by default.

You should use weak ETags if the content is semantically equal, but not byte-equal.

See also https://tools.ietf.org/html/rfc7232#section-2.3.

public self withWeakEtag ( )

                public function withWeakEtag(): self
{
    $new = clone $this;
    $new->weakEtag = true;
    return $new;
}