如何从 Yii2 中的 params.php 获取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41858561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:29:02  来源:igfitidea点击:

How to get values from params.php in Yii2

phpyii2

提问by shanthi Jagadeesh

I'm using Yii2 for my application. In params.phpfile I have defined an array like:

我在我的应用程序中使用 Yii2。在params.php文件中,我定义了一个数组,如:

return ['setValue'=>100];

And I have include params.phpin web.php:

而且我include params.phpweb.php

<?php
$params = require(__DIR__ . '/params.php');
$config = [
    'params' => $params,
];
return $config;

And I am using another file header.phpin views folder. So how can I get params array in header.php? I have used like \Yii::$app->params;, but it is not working.

我正在使用header.php视图文件夹中的另一个文件。那么我怎样才能得到 params 数组header.php呢?我用过 like \Yii::$app->params;,但它不起作用。

回答by scaisEdge

Be sure you have a proper config/main.php (this is a sample for a backend application using advanced template)

确保你有一个正确的 config/main.php(这是一个使用高级模板的后端应用程序的示例)

  <?php
    $params = array_merge(
        require(__DIR__ . '/../../common/config/params.php'),
        require(__DIR__ . '/../../common/config/params-local.php'),
        require(__DIR__ . '/params.php'),
        require(__DIR__ . '/params-local.php')
    );

    return [
        'id' => 'your-app-backend',
        'name' => 'Your APP Backend',
        'basePath' => dirname(__DIR__),
        'bootstrap' => ['log'],
        'controllerNamespace' => 'backend\controllers',
        'modules' => [],
        'components' => [
            'log' => [
                'traceLevel' => YII_DEBUG ? 3 : 0,
                'targets' => [
                    [
                        'class' => 'yii\log\FileTarget',
                        'levels' => ['error', 'warning'],
                    ],
                ],
            ],
            'errorHandler' => [
                'errorAction' => 'site/error',
            ],
        ],
        'params' => $params,
    ];

Assuming you have a param.php with

假设你有一个 param.php

<?php
  return [
    'adminEmail' => '[email protected]',
 ];

you can get the param using yii::$app->params

您可以使用 yii::$app->params 获取参数

Yii::$app->params['adminEmail'];

for printing use

印刷用

echo Yii::$app->params['adminEmail'];

回答by gabriel alejandro Rodriguez

You can access to one value with:

您可以通过以下方式访问一个值:

$value = Yii::$app->params['nameParameter'];

But, If you want to get the array

但是,如果你想得到数组

$values = Yii::$app->params;

You should be able to access all the properties defined in your config file which are integrated for use as "Yii::$app" attributes. In this case In this case the "params" attribute. :

您应该能够访问配置文件中定义的所有属性,这些属性被集成为用作“Yii::$app”属性。在这种情况下在这种情况下“params”属性。:

According to the documentation http://www.yiiframework.com/doc-2.0/guide-structure-applications.html

根据文档http://www.yiiframework.com/doc-2.0/guide-structure-applications.html