使用CakePHP 3.4和PHP 7.0。
我试图编写一个非常简单的控制器方法以输出一些JSON。但是它输出了“无法修改头文件...”。
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允许这样做。