CakePHP3.4:为什么Request :: getHeader()返回一个数组?

时间:2017-04-05 07:03:01

标签: cakephp http-headers httpresponse cakephp-3.4

我有以下要求:

2017-04-05 06:53:31 Error: Cake\Http\ServerRequest Object
(
    ...
    [_environment:protected] => Array
        (
            [HTTP_REGISTRATION] => eur73_9lhfQ:...SJMryPxCNzKcqSufdpFMOaux
            ...
        )

    [_detectorCache:protected] => Array
        (
            [ajax] => 
            [get] => 1
            [head] => 
            [options] => 
            [post] => 
        )

    [uri:protected] => Zend\Diactoros\Uri Object
        (
            [allowedSchemes:protected] => Array
                (
                    [http] => 80
                    [https] => 443
                )
            ...

        )
    ...
)

$this->request->header['registration']返回:

eur73_9lhfQ:...SJMryPxCNzKcqSufdpFMOaux

as string

$ this-> request-> getHeader('registration')返回:

[Registration] => Array
    (
        [0] => eur73_9lhfQ:...SJMryPxCNzKcqSufdpFMOaux
    )

为什么它会返回一个数组?

1 个答案:

答案 0 :(得分:1)

HTTP标准允许存在具有相同字段名称的多个标头,以防该标头的所有值都可以表示为单个逗号分隔的字符串(Set-Cookie是该规则的例外,它不能表达单个字符串中的多个值,但实际上仍然会多次出现以定义多个cookie。)

为了以方便的方式支持这一点,PSR-7标准(\Cake\Http\ServerRequest符合)定义了getHeader()方法以将标头值作为数组返回。对此的补充是getHeaderLine()方法,它将多个值作为单个逗号分隔的字符串返回。

另见