Answer the question
In order to leave comments, you need to log in
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']]);
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question