Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复传空值被忽略的情况 #2740

Merged
merged 1 commit into from
Jul 5, 2022
Merged

修复传空值被忽略的情况 #2740

merged 1 commit into from
Jul 5, 2022

Conversation

iamr0s
Copy link
Contributor

@iamr0s iamr0s commented Jul 3, 2022

当请求类型为POST,传递类型为json时,以curl为例:

curl --location --request POST 'http://test.cn/' \
--header 'Content-Type: application/json' \
--header 'Content-Length: 14' \
--data-raw '{"name": null}'

我们在控制器中,使用如下代码时

<?php

namespace app\controller;

use app\Request;

class Index {
    public function index(Request $request){
        var_dump($request->param());
        var_dump($request->only(['name']));
    }
}

返回的结果为

array(1) {
  ["name"]=> null
}
array(0) {
}

显然,在POST请求中name字段存在且为空值。但是only方法却过滤了这个值,原因是因为在Request这个类的第1493行,采用isset方法。

                if (!isset($data[$key])) {
                    continue;
                }

问题原因参考如下代码:

<?php
$data = ['name' => null];
var_dump(isset($data['name']));
var_dump(key_exists('name', $data));

输出为:

false
true

@liu21st liu21st merged commit d4ab09a into top-think:6.0 Jul 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants