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

无法在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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改