错误请求,缺少必需参数Yii2

时间:2018-05-25 20:51:00

标签: php yii2 yii2-advanced-app

我对GET请求有些问题 如果我在我的应用中发送GET请求,例如/api/v1/users/user-settings?key=model-manual-user-settings, 我得到这样的答复:

{"name":"Bad Request","message":"Missing required parameters: key", "code":0, "status":400, "type":"yii\\web\\BadRequestHttpException"}

在我的操作中运行方法

 /**
  * Получение пользовательских данных по ключу
  *
  * @param string $key Ключ пользовательских параметров
  * @return array
  */
  public function run($key)
  {
      $component = new UserSettingsComponent($key);
      return $component->getSettingsWithParams();
  }

yii2日志文件:

2018-05-25 18:58:52 [127.0.0.1][1][tsdsfjgmllh43vfb264qimc3fv][error][yii\web\HttpException:400] yii\web\BadRequestHttpException: Missing required parameters: key in /var/www/arm/vendor/yiisoft/yii2/web/Controller.php:149
Stack trace:
#0 /var/www/arm/vendor/yiisoft/yii2/base/Action.php(88): yii\web\Controller->bindActionParams(Object(api\actions\users\user_settings\IndexAction), Array)
#1 /var/www/arm/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\Action->runWithParams(Array)
#2 /var/www/arm/vendor/yiisoft/yii2/base/Module.php(528): yii\base\Controller->runAction('index', Array)
#3 /var/www/arm/vendor/yiisoft/yii2/web/Application.php(103): yii\base\Module->runAction('v1/users/user-s...', Array)
#4 /var/www/arm/vendor/yiisoft/yii2/base/Application.php(386): yii\web\Application->handleRequest(Object(yii\web\Request))
#5 /var/www/arm/api/web/index.php(15): yii\base\Application->run()
#6 {main}
2018-05-25 18:58:52 [127.0.0.1][1][tsdsfjgmllh43vfb264qimc3fv][info][application] $_GET = [
    'args' => ''
]

...

$_SERVER = [
    'USER' => 'www-data'
    'HOME' => '/var/www'
    'HTTP_X_COMPRESS' => 'null'
    'HTTP_COOKIE' => 'PHPSESSID=tsdsfjgmllh43vfb264qimc3fv; _csrf=f70915c9bfa9e77535bd94fa3287cb4347f248c120e26332eca5a1cebc7dba4ca%3A2%3A%7Bi%3A0%3Bs%3A5%3A%22_csrf%22%3Bi%3A1%3Bs%3A32%3A%22mNhgJJk1mrM_luk3c26x-dBOVUY62fpo%22%3B%7D'
    'HTTP_ACCEPT_LANGUAGE' => 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7'
    'HTTP_ACCEPT_ENCODING' => 'gzip, deflate'
    'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'
    'HTTP_USER_AGENT' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36'
    'HTTP_UPGRADE_INSECURE_REQUESTS' => '1'
    'HTTP_CACHE_CONTROL' => 'max-age=0'
    'HTTP_CONNECTION' => 'keep-alive'
    'HTTP_HOST' => 'arm.local'
    'REDIRECT_STATUS' => '200'
    'SERVER_NAME' => 'arm.local'
    'SERVER_PORT' => '80'
    'SERVER_ADDR' => '127.0.0.1'
    'REMOTE_PORT' => '42354'
    'REMOTE_ADDR' => '127.0.0.1'
    'SERVER_SOFTWARE' => 'nginx/1.10.3'
    'GATEWAY_INTERFACE' => 'CGI/1.1'
    'REQUEST_SCHEME' => 'http'
    'SERVER_PROTOCOL' => 'HTTP/1.1'
    'DOCUMENT_ROOT' => '/var/www/arm'
    'DOCUMENT_URI' => '/api/web/index.php'
    'REQUEST_URI' => '/api/v1/users/user-settings?key=wagons-array-user-settings'
    'SCRIPT_NAME' => '/api/web/index.php'
    'CONTENT_LENGTH' => ''
    'CONTENT_TYPE' => ''
    'REQUEST_METHOD' => 'GET'
    'QUERY_STRING' => 'args'
    'SCRIPT_FILENAME' => '/var/www/arm/api/web/index.php'
    'FCGI_ROLE' => 'RESPONDER'
    'PHP_SELF' => '/api/web/index.php'
    'REQUEST_TIME_FLOAT' => 1527274732.2266
    'REQUEST_TIME' => 1527274732
]

网址管理员配置:

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'enableStrictParsing' => false,
    'rules' => [
        '/settings' => '/settings',
        '/<action>' => '/site/<action>',
        '/<controller>' => '/<controller>/<action>',
        '/<controller>/<action>' => '/<controller>/<action>',
        '/<module>/<action>' => '/<module>/default/<action>',
        [
            'class' => 'yii\rest\UrlRule',
            'controller' => [
                # some controllers 
                'v1/users/user-groups',
                'v1/users/user-settings',
            ],
            'pluralize'=>false
        ],
    ],
]
nginx config的

部分

location /api/ {
    root /var/www/arm/api/web;
    try_files $uri /api/web/index.php?args;
}

我也在nginx日志文件中看到这些行:

2018/05/25 21:56:21 [error] 27040#27040: *74 FastCGI sent in stderr: "PHP message: PHP Warning:  Invalid argument supplied for foreach() in /var/www/arm/vendor/yiisoft/yii2/helpers/BaseArrayHelper.php on line 123" while reading response header from upstream, client: 127.0.0.1, server: arm.local, request: "GET /api/v1/users/user-settings?key=wagons-array-user-settings HTTP/1.1", upstream: "fastcgi://127.0.0.1:9001", host: "arm.local"
2018/05/25 21:58:52 [error] 27040#27040: *80 FastCGI sent in stderr: "PHP message: PHP Warning:  Invalid argument supplied for foreach() in /var/www/arm/vendor/yiisoft/yii2/helpers/BaseArrayHelper.php on line 123" while reading response header from upstream, client: 127.0.0.1, server: arm.local, request: "GET /api/v1/users/user-settings?key=wagons-array-user-settings HTTP/1.1", upstream: "fastcgi://127.0.0.1:9001", host: "arm.local"

请注意 它是在更新yii2到2.0.15.1和php 7.2.5之后开始的。
我尝试降级到php7.2.3,问题仍然存在 生产版我的应用程序使用yii2 v2.0.13和php 7.2.3,我没有这个问题。
如果我将yii2版本降级到2.0.13,我会收到此错误:

yiisoft/yii2-gii 2.0.7 requires yiisoft/yii2 ~2.0.14 -> satisfiable by yiisoft/yii2[2.0.x-dev]

RESP。如果我尝试将yiisoft / yii2-gii降级到v2.0.0,我会收到此错误:

The requested package yiisoft/yii2-gii (locked at 2.0.7, required as 2.0.0) is satisfiable by yiisoft/yii2-gii[2.0.7]

服务器nginx + php-fpm
Yii版本2.0.15.1
php版本7.2.5-1
nginx版本1.10

有人有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您的nginx配置中有拼写错误,在$之前错过了args

location /api/ {
    root /var/www/arm/api/web;
    try_files $uri /api/web/index.php?$args;
}