Global variable $config


(Koredalin) #1

Hello Guys,
@app/config/web.php”
How can I access the global variable $config…? A standard way would be “global $config;”. I have found that if I need something from modules subarray - I can use “\Yii::$app->getModule(‘user’)->admins;”.
Is there an elegant way to access $config array?
Thank you, All!

Original question: https://www.facebook.com/groups/yii2talk/permalink/2188273441413186/.


(Schanz15) #2

Why would you want to do that? Whats your use case?

There is no way to access the raw configuration array. At least not from Yii itself.

Anyways, these are the possibilities that I can think of:

  • Require the configuration file from whereever you need access to it (similar to how index.php includes it)
  • Put things that you want to access during application runtime to config/params.php and access them via Yii:$app->params
  • Access configuration properties through the instaniated instances during runtime (e.g. Yii::$app->request->cookieValidationKey instead of ($config['components']['request']['cookieValidationKey'])

(Koredalin) #3

Thank you for the answer, Coksnuss,
For example - I use Yii2 dektrium user extension. So, I need the admins users list. “$config[‘modules’][‘user’][‘admins’]”. An option is that I can make an additional variable $params[‘admins’], which I can use for $config[‘modules’][‘user’][‘admins’].
I didn’t found a way with any combinations of User class, like your example, Coksnuss.
Best Regards.


(Geus) #4

In some applications, we need to generate a installer for execute basics tasks; for example execute all stored migrations in files, put online general parameters for whole application like Language, Time Expiración of Session , Params from Swiftmailer …etc Without register in config/main.php else input this values from a form.