Load configs in /common/main.php from database

I have a module in backend with some configurations loaded from database; In module class init, i put the Yii$app->params and i get the following error:




public function init()

    {

        parent::init();


        try{

            $params = RedesSociaisConfig::find()->all();


            foreach($params as $param)

            {

                Yii::$app->params[$rede->rede_social] = [

                    'key' => $param->key,

                    'secret' => $param->secret,

                    'permissoes' => $param->permissoes,

                ];

            }

        } catch(yii\db\Exception $e){

            return false;

        }

    }



lcommon/main




'authClientCollection' => [

            'class' => 'yii\authclient\Collection',

            'clients' => [

                'facebook' => [

                    'class' => 'yii\authclient\clients\Facebook',

                    'authUrl' => 'https://www.facebook.com/dialog/oauth?display=popup',

                    'clientId' => \Yii::$app->params['facebook']['key'],

                    'clientSecret' =>  \Yii::$app->params['facebook']['secret'],

                    'scope'        => [

                        'email', 

                        'public_profile', 

                        'user_about_me', 

                        'user_location', 

                        'user_work_history',

                    ]

                ],

                'twitter' => [

                    'class' => 'yii\authclient\clients\Twitter',

                    'consumerKey' =>  \Yii::$app->params['twitter']['key'],

                    'consumerSecret' =>  \Yii::$app->params['twitter']['secret'], 

                ],

            ],

        ],



@edit

now i create a bootstrap file and inserted in ‘bootstrap’ key in common/main.php




'bootstrap' => [

            'backend\modules\redes_sociais\RedesSociaisBootstrap'

    ],




<?php


namespace backend\modules\redes_sociais;


use Yii;

use backend\modules\redes_sociais\models\RedesSociaisConfig;


class RedesSociaisBootstrap 

{

	public function init()

    {

        parent::init();

    }


    public function __construct()

    {   

        try{

            $params = RedesSociaisConfig::find()->all();


            foreach($params as $param)

            {

                Yii::$app->params[$param->rede_social] = [

                    'key' => $param->key,

                    'secret' => $param->secret,

                    'permissoes' => $param->permissoes,

                ];

            }


        } catch(yii\db\Exception $e){

            return false;

        }

    }

}



You can’t use \Yii::$app in config file because it was loaded before Yii app actually run.

You may consider create your own Facabook / Twitter class extending yii\authclient\clients\Facebook and yii\authclient\clients\Twitter, and initialize the config in init()

And where i should call the class with custom init configs?

You don’t need to called init()

Yii will call after constructing it and before you use it.