S
S
sdgs4s4 .2018-03-08 18:03:18
Yii
sdgs4s4 ., 2018-03-08 18:03:18

Yii2 -> how to cache a widget?

There were difficulties with caching the widget
The widget itself:

class FooterWidget extends Widget
{
    public function run()
    {
        $io = Common::findOne(['id' => '1']);

        return $this->render('footer', compact('io'));
    }
}

How to cache it? help out

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2018-03-08
@Encoderast

Can be cached as a snippet https://github.com/yiisoft/yii2/blob/master/docs/g... Something like:

if ($this->beginCache($id, ['duration' => 3600])) {
   echo \app\widgets\Menu::widget();
}

You can also use Yii::$app->cache->get('key') and Yii::$app->cache->set('key') directly in the widget body. Everything depends on the task.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question