HttpCacheTrait
Trait providing helpers to manage HTTP caching in controllers.
This trait allows controllers to:
- Optionally initialize an internal HTTP cache provider (
Slim\HttpCache\CacheProvider
). - Set or remove cache-related headers (
ETag
,Last-Modified
,Cache-Control
).
Important: To enable caching, you must call initializeHttpCache() in your controller. If you forget to initialize the cache provider, these methods will silently return the original response without adding any cache-related headers.
Table of Contents
Properties
- $httpCache : CacheProvider|null
- The cache provider reference (optional).
Methods
- allowCache() : ResponseInterface
- Enable HTTP caching for the given response.
- denyCache() : ResponseInterface
- Enforce the removal of browser cache for a response.
- initializeHttpCache() : static
- Initialize the internal HTTP cache provider.
- withEtag() : ResponseInterface
- Add an `ETag` header to a PSR-7 response object.
- withExpires() : ResponseInterface
- Add an `Expires` header to a PSR-7 response object.
- withLastModified() : ResponseInterface
- Add a `Last-Modified` header to a PSR-7 response object.
Properties
$httpCache
The cache provider reference (optional).
protected
CacheProvider|null
$httpCache
= null
When set, this property holds a Slim\HttpCache\CacheProvider
instance
used to modify HTTP response headers for caching.
Methods
allowCache()
Enable HTTP caching for the given response.
public
allowCache(ResponseInterface $response[, string $type = 'private' ][, int|string|null $maxAge = null ][, bool $mustRevalidate = false ]) : ResponseInterface
This method sets a Cache-Control
header using the underlying CacheProvider.
If the HTTP cache provider is not initialized, the response is returned unchanged.
Parameters
- $response : ResponseInterface
-
A PSR-7 response object
- $type : string = 'private'
-
Cache-Control type: "private" or "public"
- $maxAge : int|string|null = null
-
Maximum cache age in seconds or a datetime string parsable by strtotime()
- $mustRevalidate : bool = false
-
Whether to add the "must-revalidate" directive
Return values
ResponseInterface —A new response object with cache headers if the cache provider is available.
denyCache()
Enforce the removal of browser cache for a response.
public
denyCache(ResponseInterface $response) : ResponseInterface
Equivalent to setting headers like Cache-Control: no-store, no-cache, must-revalidate
.
If the HTTP cache provider is not initialized, the response is returned unchanged.
Parameters
- $response : ResponseInterface
-
A PSR-7 response object
Return values
ResponseInterface —A new response object with cache denial headers if available.
initializeHttpCache()
Initialize the internal HTTP cache provider.
public
initializeHttpCache([array<string|int, mixed> $init = [] ][, ContainerInterface|null $container = null ]) : static
Priority order:
$init[ControllerParam::HTTP_CACHE]
$container->get(CacheProvider::class)
if available in DI
Parameters
- $init : array<string|int, mixed> = []
-
Optional initialization array
- $container : ContainerInterface|null = null
-
Optional DI container to retrieve the cache provider
Tags
Return values
static —Returns the current instance for method chaining
withEtag()
Add an `ETag` header to a PSR-7 response object.
public
withEtag(ResponseInterface $response, string $value[, string $type = 'strong' ]) : ResponseInterface
The ETag
is used by browsers and proxies for cache validation.
If the HTTP cache provider is not initialized, the response is returned unchanged.
Parameters
- $response : ResponseInterface
-
A PSR-7 response object
- $value : string
-
The ETag value
- $type : string = 'strong'
-
The ETag type: either
"strong"
or"weak"
Return values
ResponseInterface —A new response object with the ETag
header set if available
withExpires()
Add an `Expires` header to a PSR-7 response object.
public
withExpires(ResponseInterface $response, string|int $time) : ResponseInterface
This header specifies the date and time after which the response is considered stale. If the HTTP cache provider is not initialized, the response is returned unchanged.
Parameters
- $response : ResponseInterface
-
A PSR-7 response object
- $time : string|int
-
A UNIX timestamp or a string compatible with
strtotime()
.
Return values
ResponseInterface —A new response object with the Expires
header set if available
withLastModified()
Add a `Last-Modified` header to a PSR-7 response object.
public
withLastModified(ResponseInterface $response, int|string $time) : ResponseInterface
This header informs caches of the last modification date of the resource. If the HTTP cache provider is not initialized, the response is returned unchanged.
Parameters
- $response : ResponseInterface
-
A PSR-7 response object
- $time : int|string
-
A UNIX timestamp or a string compatible with
strtotime()
Return values
ResponseInterface —A new response object with the Last-Modified
header set if available