Yii2 params access within local config file in common directory -


i'm using yii2 advanced template, want access params.php in main-local.php file, called ways:

main-local.php:

'mailer' => [             'class' => 'myclass',              'apikey' => \yii::$app->params['mandrill_api_key'],              'viewpath' => '@common/mail',                     ], 

and have stored mandrill_api_key in params.php

params.php:

<?php return [     'adminemail' => 'admin@example.com',     'supportemail' => 'support@example.com',     'user.passwordresettokenexpire' => 3600,      'mandrill_api_key' => 'mykey' ]; 

i'm getting error:

notice: trying property of non-object in c:\xampp\htdocs\myproject\common\config\main-local.php on line 25

what should access these parameters?

the config files read before application instantiated explained in request lifecycle:

  1. a user makes request entry script web/index.php.
  2. the entry script loads application configuration , creates application instance handle request.
  3. the application resolves requested route of request application component.
  4. ...

as such \yii::$app not yet exist hence error. suggest moving api_key definition main-local.php config such there no confusion on being set:

'mailer' => [     'class' => 'myclass',     'apikey' => 'actual api key',     'viewpath' => '@common/mail',             ], 

alternatively, can use yii2's dependancy injection container set apikey in application's entry script:

... $app = new yii\web\application($config); \yii::$container->set('\fully\qualified\myclass', [     'apikey' => \yii::$app->params['mandrill_api_key'], ]); $app->run(); 

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -