A
A
akula222016-02-06 11:13:37
Yii
akula22, 2016-02-06 11:13:37

Where should I register getting settings from the cache?

I store the site settings in the admin in the cache
$cache->set('settings', $model->getAttributes());
Now I want to display data from the settings on the site, for example, specify the title for the page /site/default/index.
Question: where should I register getting the settings from the cache
$settings = Yii::$app->cache->get('settings');
So that for example in the /site/default/index. Get them

$this->title = $settings['title'];
$this->registerMetaTag(['name' => 'keywords', 'content' => $settings['keywords']]);
$this->registerMetaTag(['name' => 'description', 'content' => $settings['description']]);

I understand that I can do this in the /site/default controller,
but I want to do it once and globally, for example, on the contacts page, I will need to remove the phones from the cache, and I would not like to receive data from the cache again in the contacts controller.
Give advice where to do it once and globally

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2016-02-06
@akula22

Anywhere you can through Yii::$app->cache->get('settings');
If you need it everywhere, you can inherit all controllers from one, for example MainController and do in it:

public function beforeAction($event)
    {
        $data = Yii::$app->cache->get('settings');
        if ($data === false) {
            $data = $cache->set('settings', $model->getAttributes());
            Yii::$app->cache->set('settings', $data, 3600);
        }
// как вариант можно записывать в Yii::$app->params
//        Yii::$app->params = $data;

        return parent::beforeAction($event);
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question