Symfony2中没有使用Response对象修改标头

Mar*_*cía 1 symfony

我试图使用Symfony 2将PDF返回到浏览器,所以一旦找到了文件,我就会使用:

return new Response(readfile($file_path), 200, array(
'Content-Type' => 'application/pdf'
));
Run Code Online (Sandbox Code Playgroud)

但如果我加载页面,标题不会被修改,内容不会被解释为PDF格式...

< HTTP/1.1 200 OK
< Date: Sat, 31 Mar 2012 20:39:20 GMT
< Server: Apache
< X-Powered-By: PHP/5.3.6
< Set-Cookie: PHPSESSID=XXXX; path=/
< Transfer-Encoding: chunked
< Content-Type: text/html
Run Code Online (Sandbox Code Playgroud)

我迷失了这个问题.有什么想法吗?

Asa*_*ers 7

readfile首先执行,然后开始将文件发送到客户端.这会触发生成标头.然后传递readfile的返回值Response.当Response返回给客户端时,PHP无法更改标头,因为它们在readfile运行时被触发.

替换readfilefile_get_contents.