Skip to content
PHP Ecosystem
Explore

FragmentCache

API reference › yii › widgets › FragmentCache

FragmentCache is used by View to provide caching of page fragments.

PropertyTypeDescriptionDefined by
$autoIdPrefix staticstringThe prefix to the automatically generated widget IDs. Default: 'w'.yii\base\Widget
$behaviors read-onlyarray<yii\base\Behavior<static>>List of behaviors attached to this component.yii\base\Component
$cacheyii\caching\CacheInterface|array|stringThe cache object or the application component ID of the cache object. Default: 'cache'.FragmentCache
$cachedContent read-onlystring|falseThe cached content.FragmentCache
$dependencyarray|yii\caching\DependencyThe dependency that the cached content depends on. Default: null.FragmentCache
$durationintNumber of seconds that the data can remain valid in cache. Default: 60.FragmentCache
$dynamicPlaceholdersarrayA list of placeholders.yii\base\DynamicContentAwareTrait
$enabledboolWhether to enable the fragment cache. Default: true.FragmentCache
$idstringId of the widget.yii\base\Widget
$variationsstring[]|stringList of factors that would cause the variation of the content being cached. Default: null.FragmentCache
$viewyii\web\ViewThe view object that can be used to render views or view files.yii\base\Widget
$viewPath read-onlystringThe directory containing the view files for this widget.yii\base\Widget
MethodDescriptionDefined by
__construct()Constructor.yii\base\BaseObject
__call()Calls the named method which is not a class method.yii\base\Component
__clone()This method is called after the object is created by cloning an existing one.yii\base\Component
__get()Returns the value of a component property.yii\base\Component
__isset()Checks if a property is set, i.e. defined and not null.yii\base\Component
__set()Sets the value of a component property.yii\base\Component
__unset()Sets a component property to be null.yii\base\Component
addDynamicPlaceholder()Adds a placeholder for dynamic content.yii\base\DynamicContentAwareTrait
afterRun()This method is invoked right after a widget is executed.yii\base\Widget
attachBehavior()Attaches a behavior to this component.yii\base\Component
attachBehaviors()Attaches a list of behaviors to the component.yii\base\Component
beforeRun()This method is invoked right before the widget is executed.yii\base\Widget
begin() staticBegins a widget.yii\base\Widget
behaviors()Returns a list of behaviors that this component should behave as.yii\base\Component
canGetProperty()Returns a value indicating whether a property can be read.yii\base\Component
canSetProperty()Returns a value indicating whether a property can be set.yii\base\Component
className() staticReturns the fully qualified name of this class.yii\base\BaseObject
detachBehavior()Detaches a behavior from the component.yii\base\Component
detachBehaviors()Detaches all behaviors from the component.yii\base\Component
end() staticEnds a widget.yii\base\Widget
ensureBehaviors()Makes sure that the behaviors declared in behaviors() are attached to this component.yii\base\Component
getBehavior()Returns the named behavior object.yii\base\Component
getBehaviors()Returns all behaviors attached to this component.yii\base\Component
getCachedContent()Returns the cached content if available.FragmentCache
getDynamicPlaceholders()Returns a list of placeholders for dynamic content. This method is used internally to implement the content caching feature.yii\base\DynamicContentAwareTrait
getId()Returns the ID of the widget.yii\base\Widget
getView()Returns the view object that can be used to render views or view files.yii\base\Widget
getViewPath()Returns the directory containing the view files for this widget.yii\base\Widget
hasEventHandlers()Returns a value indicating whether there is any handler attached to the named event.yii\base\Component
hasMethod()Returns a value indicating whether a method is defined.yii\base\Component
hasProperty()Returns a value indicating whether a property is defined for this component.yii\base\Component
init()Initializes the FragmentCache object.FragmentCache
off()Detaches an existing event handler from this component.yii\base\Component
on()Attaches an event handler to an event.yii\base\Component
render()Renders a view.yii\base\Widget
renderFile()Renders a view file.yii\base\Widget
run()Marks the end of content to be cached.FragmentCache
setDynamicPlaceholders()Sets a list of placeholders for dynamic content. This method is used internally to implement the content caching feature.yii\base\DynamicContentAwareTrait
setId()Sets the ID of the widget.yii\base\Widget
setView()Sets the view object to be used by this widget.yii\base\Widget
trigger()Triggers an event.yii\base\Component
widget() staticCreates a widget instance and runs it.yii\base\Widget
calculateKey() protectedGenerates a unique key used for storing the content in cache.FragmentCache
updateDynamicContent() protectedReplaces placeholders in $content with results of evaluated dynamic statements.yii\base\DynamicContentAwareTrait
EventTypeDescriptionDefined by
EVENT_INITyii\base\EventAn event that is triggered when the widget is initialized via init(). Event name: 'init'. Available since 2.0.11.yii\base\Widget
EVENT_BEFORE_RUNyii\base\WidgetEventAn event raised right before executing a widget. Event name: 'beforeRun'. Available since 2.0.11.yii\base\Widget
EVENT_AFTER_RUNyii\base\WidgetEventAn event raised right after executing a widget. Event name: 'afterRun'. Available since 2.0.11.yii\base\Widget
public yii\caching\CacheInterface|array|string $cache = 'cache'

The cache object or the application component ID of the cache object.

After the FragmentCache object is created, if you want to change this property, you should only assign it with a cache object. Starting from version 2.0.2, this can also be a configuration array for creating the object.

public string|false $cachedContent

The cached content.

False is returned if valid content is not found in the cache.

Access

Read-only (virtual property).

public array|yii\caching\Dependency $dependency = null

The dependency that the cached content depends on.

This can be either a Dependency object or a configuration array for creating the dependency object. For example,

[
'class' => 'yii\caching\DbDependency',
'sql' => 'SELECT MAX(updated_at) FROM post',
]

would make the output cache depends on the last modified time of all posts. If any post has its modification time changed, the cached content would be invalidated.

public int $duration = 60

Number of seconds that the data can remain valid in cache.

Use 0 to indicate that the cached data will never expire.

public bool $enabled = true

Whether to enable the fragment cache.

You may use this property to turn on and off the fragment cache according to specific setting (e.g. enable fragment cache only for GET requests).

public string[]|string $variations = null

List of factors that would cause the variation of the content being cached.

Each factor is a string representing a variation (e.g. the language, a GET parameter). The following variation setting will cause the content to be cached in different versions according to the current application language:

[
Yii::$app->language,
]
public function getCachedContent(): string|false

Returns the cached content if available.

Returns

string|false: The cached content. False is returned if valid content is not found in the cache.

public function init(): mixed

Initializes the FragmentCache object.

Returns

mixed

public function run(): mixed

Marks the end of content to be cached.

Content displayed before this method call and after init() will be captured and saved in cache. This method does nothing if valid content is already found in cache.

Returns

mixed

protected function calculateKey(): mixed

Generates a unique key used for storing the content in cache.

The key generated depends on both $id and $variations.

Returns

mixed: A valid cache key