无法在API控制器Yii2中获取配置参数值

时间:2019-05-22 07:00:51

标签: php api parameters yii2

调用Yii::$app->params['status']时出现不确定的参数错误

我已经创建了用于Web服务的api文件夹,在这个文件夹中,我有api/controllers/UsersController.php。我在此控制器的一项操作中称此Yii::$app->params['status'],但收到未定义的错误。

这是我的api/config/main.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' => 'api',
    'basePath' => dirname(__DIR__),
    'controllerNamespace' => 'api\controllers',
   'bootstrap' => ['log'],
      'components' => [
        'user' => [
            'identityClass' => 'common\models\Users',
            'enableAutoLogin' => false,
            'idParam' => '_api'
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
        ],
        'response' => [
            'class' => 'yii\web\Response',
            'on beforeSend' => function ($event) {
                $response = $event->sender;
                if ($response->data !== null && Yii::$app->request->get('suppress_response_code')) {
                    $response->data = [
                        'success' => $response->isSuccessful,
                        'data' => $response->data,
                    ];
                    $response->statusCode = 200;
                }
            },
        ],
        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => 'pbB0NvlmxlWRk7XFCN_7XUC2uvX0vyCD',
        ],
    ],

];

if (!YII_ENV_TEST) {
    // configuration adjustments for 'dev' environment
//    $config['bootstrap'][] = 'debug';
//    $config['modules']['debug'] = 'yii\debug\Module';
}

return $params;

这是我的api/web/index.php

defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');

require(__DIR__ . '/../../vendor/autoload.php');
require(__DIR__ . '/../../vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/../../common/config/aliases.php');
require(__DIR__ . '/../../common/config/params.php');


//require(__DIR__ . '/../../common/twiliophp/Services/Twilio.php');

$config = yii\helpers\ArrayHelper::merge(
                require(__DIR__ . '/../../common/config/main.php'),
                // require(__DIR__ . '/../../common/config/main-local.php'),
                require(__DIR__ . '/../config/main.php')
                // require(__DIR__ . '/../config/main-local.php')
);
require(__DIR__ . '/../../common/config/aliases.php');

$application = new yii\web\Application($config);
$application->run();

我还在common\config\params.php中定义了以下状态:

return [
   //  'bsVersion' => '4.x',
     'bsDependencyEnabled' => 'false',
   'status' => array('1' => 'Active', '0' => 'In-Active')
];

请告诉我我犯错的人。

1 个答案:

答案 0 :(得分:1)

您的配置文件有问题。 您将返回主配置对象,几行之后,您将返回参数。

只需按以下步骤更改配置文件:

$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')
);

$config = [
    'id' => 'api',
    'basePath' => dirname(__DIR__),
    'controllerNamespace' => 'api\controllers',
    'bootstrap' => ['log'],
    'components' => [
        'user' => [
            'identityClass' => 'common\models\Users',
            'enableAutoLogin' => false,
            'idParam' => '_api'
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
        ],
        'response' => [
            'class' => 'yii\web\Response',
            'on beforeSend' => function ($event) {
                $response = $event->sender;
                if ($response->data !== null && Yii::$app->request->get('suppress_response_code')) {
                    $response->data = [
                        'success' => $response->isSuccessful,
                        'data' => $response->data,
                    ];
                    $response->statusCode = 200;
                }
            },
        ],
        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => 'pbB0NvlmxlWRk7XFCN_7XUC2uvX0vyCD',
        ],
    ],
    'params' => $params
];
if (!YII_ENV_TEST) {
    // configuration adjustments for 'dev' environment
//    $config['bootstrap'][] = 'debug';
//    $config['modules']['debug'] = 'yii\debug\Module';
}
return $config;

此外,索引文件上的这一行是多余的:

require(__DIR__ . '/../../common/config/params.php');