duanqian6982
2019-05-22 07:00
浏览 244
已采纳

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

I am getting undefined parameter error while calling Yii::$app->params['status']

I have created api folder for web services and in this i have api/controllers/UsersController.php. I am calling this Yii::$app->params['status'] in one of the action of this controller but getting undefined error.

This is my 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;

This is my 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();

Also i have defined status in common\config\params.php as follows :

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

Please tell someone when i made mistake.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dqiaw48488 2019-05-22 07:16
    已采纳

    You have a problem in your config file. You're returning the main config object, and few rows later you're returning the params.

    Just change your config file as follows:

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

    Also, this line on the index file is redundant:

    require(__DIR__ . '/../../common/config/params.php');
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题