如何使用 CakePHP 3.4 输出自定义 HTTP 正文内容?回显导致 "Unable to emit headers" 错误

How to output custom HTTP body contents with CakePHP 3.4? Echoing causes "Unable to emit headers" error

使用 CakePHP3.4,PHP7.0。

我正在尝试做一个非常简单的控制器方法来输出一些 JSON。它正在输出 "Cannot modify headers...".

public function test() {
    $this->autoRender = false;
    echo json_encode(['method' => __METHOD__, 'class' => get_called_class()]);
}

浏览器输出

{"method":"App\Controller\SomeController::test", "class":"App\Controller\SomeController"}

Warning (512): Unable to emit headers. Headers sent in file=...
Warning (2): Cannot modify header information - headers already sent by (output started at ...)
Warning (2): Cannot modify header information - headers already sent by (output started at ...)

我完全理解为什么 PHP 会抱怨这个。问题是为什么 CakePHP 会抱怨,我该怎么办?

需要注意的是 CakePHP 2.x 允许这样做。

CakePHP 3 有一个叫做 JSON views 的东西,它允许你 return JSON 数据。我以前没有做过任何 CakePHP,所以我不知道请求的生命周期,但值得研究一下。

控制器不应该回显数据!回显数据会导致各种问题,从测试环境无法识别数据,到header无法发送,甚至数据被截断。

在 CakePHP 中这样做已经是错误的 2.x,尽管它可能在某些甚至大多数情况下都有效。随着新 HTTP 堆栈的引入,CakePHP 现在在回显响应之前明确检查已发送的 headers,并相应地触发错误。

发送自定义输出的正确方法是配置 return 响应 object,或者使用序列化视图,在 3.x.[=28 中仍然是相同的=]

引自文档:

Controller actions generally use Controller::set() to create a context that View uses to render the view layer. Because of the conventions that CakePHP uses, you don’t need to create and render the view manually. Instead, once a controller action has completed, CakePHP will handle rendering and delivering the View.

If for some reason you’d like to skip the default behavior, you can return a Cake\Network\Response object from the action with the fully created response.

* 从 3.4 开始就是 \Cake\Http\Response

Cookbook > Controllers > Controller Actions

配置响应

使用 PSR-7 兼容接口

$content = json_encode(['method' => __METHOD__, 'class' => get_called_class()]);

$this->response = $this->response->withStringBody($content);
$this->response = $this->response->withType('json');
// ...

return $this->response;

PSR-7 兼容接口使用不可变方法,因此利用了 withStringBody()withType() 的 return 值。在 CakePHP < 3.4.3 中,withStringBody() 不可用,您可以直接写入 body 流,这不会改变响应的状态 object:

$this->response->getBody()->write($content);

使用已弃用的界面

$content = json_encode(['method' => __METHOD__, 'class' => get_called_class()]);

$this->response->body($content);
$this->response->type('json');
// ...

return $this->response;

使用序列化视图

$content = ['method' => __METHOD__, 'class' => get_called_class()];

$this->set('content', $content);
$this->set('_serialize', 'content');

这还需要使用请求处理程序组件,并启用扩展解析和使用附加 .json 的相应 URL,或者发送带有 application/json 接受 [=81= 的正确请求].

另见