Hello! First of all, sorry for my bad english.
I use Yii2 starter kit and i want to get language buttons from database.
I made LanguageQuery model to common directory where i made method "locales":
public static function locales()
{
return ArrayHelper::map(Language::find()->select(['code', 'title'])->all(), 'code', 'title');
}
Now i want to edit layout file. Original is here: https://github.com/trntv/yii2-starter-kit/blob/master/frontend/views/layouts/base.php
And i tryed to change lines 49-58:
[
'label'=>Yii::t('frontend', 'Language'),
'items'=>array_map(function ($code) {
return [
'label' => \common\models\query\LanguageQuery::locales(), #what to write here?
'url' => ['/site/set-locale', 'locale'=>$code],
'active' => Yii::$app->language === $code
];
}, array_keys(\common\models\query\LanguageQuery::locales()))
]
Now link works well, but label is missing. I dont understand who to get label.
And is my approach right or im doing this completely wrong?
EDIT:
Changed the code and now it works:
[
'label'=>Yii::t('frontend', 'Language'),
'items'=>array_map(function ($code) {
return [
'label' => \common\models\query\LanguageQuery::locales()[$code],
'url' => ['/site/set-locale', 'locale'=>$code],
'active' => Yii::$app->language === $code
];
}, array_keys(\common\models\query\LanguageQuery::locales()))
]
But i still want to know from smarter guys is this approach ok or whats the best practise.