Symfony2从控制器响应中删除UTF-8 BOM [英] Symfony2 Remove UTF-8 BOM from controller responses

查看:87
本文介绍了Symfony2从控制器响应中删除UTF-8 BOM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建强制下载"操作时,我发现控制器生成的所有响应都包括UTF-8 BOM().这与常规页面无关,但是对于下载的文件则是不可取的,因为据报告,对于某些Windows查看软件,JPG或ZIP损坏.因此,主要目标是从控制器输出中删除BOM.

While creating a "force download" action I discovered that ALL responses generated by the controller are including the UTF-8 BOM (). This is not relevant for regular pages, but for downloaded files is undesirable, since JPG or ZIP are reported as corrupt for several Windows viewing softwares. So, the main goal is to remove the BOM from controller output.

直到现在,我已经做到了:

Until now I've done this:

1-使用Win Grep在我网站上的每个文件上搜索BOM块.零结果.

1-Use Win Grep to search for BOM chunk on every file on my site. Zero results.

2-在同一Web服务器上创建一个非symfony的test.php,并检查客户端上的输出.那里没有物料清单.

2-Create a non-symfony test.php on the same web server and check output on the client. No BOM there.

有什么想法吗?

预先感谢, Z

更新:测试代码1.生成的JPG包含BOM.

UPDATE: Test Code 1. The resulting JPG includes the BOM.

public function downloadAction(Request $request){
    $filename= 'test.jpg';
    $response = new Response(file_get_contents($filename));
    $response->headers->set('Content-Type',mime_content_type($filename));        
    $response->headers->set('Content-Disposition', 'attachment; filename="' . basename($filename) . '"');
   return $response;

}

测试代码2.结果页面还包含BOM.

Test Code 2. The resulting page also includes the BOM.

public function downloadAction(Request $request){
echo 'hello world.';
exit; 

}

UPDATE2:我刚刚尝试过:从头开始创建一个全新的symfony2项目,使用相同的IDE添加一个测试控制器/动作,然后猜测:没有BOM..so,我的猜测是我symfony网站中的某些内容正在被拦截响应并添加BOM表块.

UPDATE2: I just tried this: create a brand new symfony2 project from scratch, added a test controller/action using same IDE, and guess what: NO BOM..so, my guess is that something inside my symfony site is intercepting the response and adding the BOM chunk.

推荐答案

著名的UTF-8 BOM已由PEAR下的LocalizeRoute.php树枝扩展添加.在我的特殊情况下,它接缝了Symfony通过该文件过滤所有响应的过程,将BOM表块添加到已交付的每个页面中.从LocalizeRoute.php删除BOM足以解决此问题.感谢nifr提出了BOM搜索建议.使用免费的窗口工具WinGrep在整个服务器上运行二进制搜索.非常感谢.

The famous UTF-8 BOM had been added by LocalizeRoute.php twig extension under PEAR. In my particular case it seams Symfony filters all responses through this file, adding the BOM chunk to every page been delivered. Removing the BOM from LocalizeRoute.php was enough to fix the problem. Credits to nifr for suggesting the BOM-search. Used free window's tool WinGrep to run the binary search through the whole server. Thanks a lot.

这篇关于Symfony2从控制器响应中删除UTF-8 BOM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆