2017-06-22 60 views
0

我有一个奇怪的问题,由此我看到了下面的PHP警告:CakePHP 3 - 由于内容长度而显示警告?

Warning (512): Unable to emit headers. Headers sent in file=/vendor/cakephp/cakephp/src/Error/Debugger.php line=921 [CORE/src/Http/ResponseEmitter.php, line 48] 
Code Context 
Cake\Http\ResponseEmitter::emit() - CORE/src/Http/ResponseEmitter.php, line 48 
Cake\Http\Server::emit() - CORE/src/Http/Server.php, line 105 
[main] - ROOT/webroot/index.php, line 37 
Warning (2): Cannot modify header information - headers already sent by (output started at /vendor/cakephp/cakephp/src/Error/Debugger.php:921) [CORE/src/Http/ResponseEmitter.php, line 148] 
Code Context 
header - [internal], line ?? 
Cake\Http\ResponseEmitter::emitStatusLine() - CORE/src/Http/ResponseEmitter.php, line 148 
Cake\Http\ResponseEmitter::emit() - CORE/src/Http/ResponseEmitter.php, line 54 
Cake\Http\Server::emit() - CORE/src/Http/Server.php, line 105 
[main] - ROOT/webroot/index.php, line 37 
Warning (2): Cannot modify header information - headers already sent by (output started at /vendor/cakephp/cakephp/src/Error/Debugger.php:921) [CORE/src/Http/ResponseEmitter.php, line 181] 
Code Context 
header - [internal], line ?? 
Cake\Http\ResponseEmitter::emitHeaders() - CORE/src/Http/ResponseEmitter.php, line 181 
Cake\Http\ResponseEmitter::emit() - CORE/src/Http/ResponseEmitter.php, line 55 
Cake\Http\Server::emit() - CORE/src/Http/Server.php, line 105 
[main] - ROOT/webroot/index.php, line 37 

我在开发模式('DEBUG', trueapp/config.php)是。如果我将其设置为false,则警告消失,如预期。

错误是由该控制器方法始发:

public function ajaxSubstances() 
{ 
    $this->autoRender = false; 
    $SubstanceModel = new SubstanceModel; 
    $data = ['data' => $SubstanceModel->getSubstances()]; 
    debug($data); 
} 

我不明白的是,如果我的$data数组只包含,说20元,没有警告显示。随着我逐渐添加更多元素,一旦有一定数量的记录(大约> = 30个元素),它会始终显示警告。

要检查它不是源数据(来自getSubstances())我硬编码了一个$data数组,但同样的事情发生。

此方法将被用来作为Ajax响应和被馈送到数据表。不幸的是,如果输出警告,DataTables将显示错误,因为它不是有效的响应。

我无法将debug更改为false,因为我需要在我的其他开发中使用该功能。我想知道为什么这可能会发生呢?

我不知道为什么它的尝试,因为头型不被任何地方的代码改为“发射头” - 它的使用debug()输出的一切,我也试过var_dump()但输出仍然是相同的text/html内容类型。

回答

1

这不一定是由于内容的长度,但通常是由于发射前,输出数据可以输出头,它就会发生一次甚至有响应发射器进场,其中明确测试之前发送一个字节为headers_sent()

注意,有另外的标题可能需要除了那些你可能已经应用到自己的反应,如饼干,内容长度被发射等

你最有可能看到的只是警告是的一定量的数据,因为你正在使用PHP的输出缓冲和/或压缩(请参阅您php.inioutput_bufferingzlib.output_compression),这只会在缓冲存储能力被超过的情况下,该脚本之中自动刷新数据(在大多数通常是4096字节的情况)。

+0

感谢一如既往对您有所帮助。我需要输出的数据需要是JSON提要。我配置为https://book.cakephp.org/3.0/en/views/json-and-xml-views.html描述这似乎已经帮助JSON的意见 - 内容类型头被适当地设置,我可以输出任何数量的数据没有问题,所以它似乎到目前为止! – Andy