PHP Class yii\filters\HttpCache

It is an action filter that can be added to a controller and handles the beforeAction event. To use HttpCache, declare it in the behaviors() method of your controller class. In the following example the filter will be applied to the list-action and the Last-Modified header will contain the date of the last update to the user table in the database. php public function behaviors() { return [ [ 'class' => 'yii\filters\HttpCache', 'only' => ['index'], 'lastModified' => function ($action, $params) { $q = new \yii\db\Query(); return $q->from('user')->max('updated_at'); }, 'etagSeed' => function ($action, $params) { return // generate ETag seed here } ], ]; }
Since: 2.0
Author: Da:Sourcerer ([email protected])
Author: Qiang Xue ([email protected])
Inheritance: extends yii\base\ActionFilter
Show file Open project: yiisoft/yii2 Class Usage Examples

Public Properties

Property Type Description
$cacheControlHeader the value of the Cache-Control HTTP header. If null, the header will not be sent.
$enabled a value indicating whether this filter should be enabled.
$etagSeed a PHP callback that generates the ETag seed string. The callback's signature should be: php function ($action, $params) where $action is the [[Action]] object that this filter is currently handling; $params takes the value of [[params]]. The callback should return a string serving as the seed for generating an ETag.
$lastModified a PHP callback that returns the UNIX timestamp of the last modification time. The callback's signature should be: php function ($action, $params) where $action is the [[Action]] object that this filter is currently handling; $params takes the value of [[params]]. The callback should return a UNIX timestamp.
$params additional parameters that should be passed to the [[lastModified]] and [[etagSeed]] callbacks.
$sessionCacheLimiter the name of the cache limiter to be set when session_cache_limiter() is called. The default value is an empty string, meaning turning off automatic sending of cache headers entirely. You may set this property to be public, private, private_no_expire, and nocache. Please refer to session_cache_limiter() for detailed explanation of these values. If this property is null, then session_cache_limiter() will not be called. As a result, PHP will send headers according to the session.cache_limiter PHP ini setting.
$weakEtag whether to generate weak ETags. Weak ETags should be used if the content should be considered semantically equivalent, but not byte-equal.

Public Methods

Method Description
beforeAction ( Action $action ) : boolean This method is invoked right before an action is to be executed (after all possible filters.) You may override this method to do last-minute preparation for the action.

Protected Methods

Method Description
generateEtag ( string $seed ) : string Generates an ETag from the given seed string.
sendCacheControlHeader ( ) Sends the cache control header to the client
validateCache ( integer $lastModified, string $etag ) : boolean Validates if the HTTP cache contains valid content.

Method Details

beforeAction() public method

This method is invoked right before an action is to be executed (after all possible filters.) You may override this method to do last-minute preparation for the action.
public beforeAction ( Action $action ) : boolean
$action yii\base\Action the action to be executed.
return boolean whether the action should continue to be executed.

generateEtag() protected method

Generates an ETag from the given seed string.
protected generateEtag ( string $seed ) : string
$seed string Seed for the ETag
return string the generated ETag

sendCacheControlHeader() protected method

Sends the cache control header to the client
See also: cacheControlHeader
protected sendCacheControlHeader ( )

validateCache() protected method

If both Last-Modified and ETag are null, returns false.
protected validateCache ( integer $lastModified, string $etag ) : boolean
$lastModified integer the calculated Last-Modified value in terms of a UNIX timestamp. If null, the Last-Modified header will not be validated.
$etag string the calculated ETag value. If null, the ETag header will not be validated.
return boolean whether the HTTP cache is still valid.

Property Details

$cacheControlHeader public property

the value of the Cache-Control HTTP header. If null, the header will not be sent.
See also: http://tools.ietf.org/html/rfc2616#section-14.9
public $cacheControlHeader

$enabled public property

a value indicating whether this filter should be enabled.
public $enabled

$etagSeed public property

a PHP callback that generates the ETag seed string. The callback's signature should be: php function ($action, $params) where $action is the [[Action]] object that this filter is currently handling; $params takes the value of [[params]]. The callback should return a string serving as the seed for generating an ETag.
public $etagSeed

$lastModified public property

a PHP callback that returns the UNIX timestamp of the last modification time. The callback's signature should be: php function ($action, $params) where $action is the [[Action]] object that this filter is currently handling; $params takes the value of [[params]]. The callback should return a UNIX timestamp.
See also: http://tools.ietf.org/html/rfc7232#section-2.2
public $lastModified

$params public property

additional parameters that should be passed to the [[lastModified]] and [[etagSeed]] callbacks.
public $params

$sessionCacheLimiter public property

the name of the cache limiter to be set when session_cache_limiter() is called. The default value is an empty string, meaning turning off automatic sending of cache headers entirely. You may set this property to be public, private, private_no_expire, and nocache. Please refer to session_cache_limiter() for detailed explanation of these values. If this property is null, then session_cache_limiter() will not be called. As a result, PHP will send headers according to the session.cache_limiter PHP ini setting.
public $sessionCacheLimiter

$weakEtag public property

whether to generate weak ETags. Weak ETags should be used if the content should be considered semantically equivalent, but not byte-equal.
See also: http://tools.ietf.org/html/rfc7232#section-2.3
Since: 2.0.8
public $weakEtag