如何在Yii2 Framework的Controller Console中获取配置参数

时间:2016-07-21 08:29:39

标签: yii2

如何在Yii2 Framework中的控制台控制器中获取配置Uncaught TypeError: browser.isExisting(...).then is not a function

我尝试下面的代码,但它不起作用

params

3 个答案:

答案 0 :(得分:2)

试试这个:

config / params.php中的代码

    <?php
    return   array(
                    'apptitle' => 'stackOverlfow', 
                   //Define PARAMS as you need.
                );
    ?>

您可以使用以下PARAM:

\Yii::$app->params['apptitle'];

示例:

echo "App title is:". \Yii::$app->params['apptitle']; 

答案 1 :(得分:2)

如您所述基本模板 Yii

<强>配置/ web.php

<?php

$params = require(__DIR__ . '/params.php');

$config = [
  'id' => 'basic',
  'basePath' => dirname(__DIR__),
  'bootstrap' => ['log'],
  'components' => [
    .
    .
    .
  ],
  'params' => $params,
];

return $config;
?>

<强>配置/ params.php

<?php
$params = [
  'params_1' => 'YourValue'
];

return $params;
?>

<强> SomeWhere.php

<?=Yii::$app->params['params_1'];?>

答案 2 :(得分:1)

似乎你将你的param插入其他conf params.php。  我的助手功能:

/**
 * Get param value from config file.
 * Получение параметра из конфигурационного файла
 *
 * @param string $param_name название пареметра
 *
 * @return string|ApicoServerErrorHttpException Значение параметра
 * @throws \Exception
 */
public static function yiiparam($param_name)
{
    if (isset(\Yii::$app->params[$param_name])) {
        return \Yii::$app->params[$param_name];
    } else {
        $msg = "Can not find param in configuration file. have been search by param = " . VarDumper::export($param_name);
        \Yii::error($msg, __METHOD__);
        throw new ServerErrorHttpException();
    }
}