【发布时间】:2014-11-18 07:53:33
【问题描述】:
在我的控制器函数中,我需要根据接受的内容类型和请求的发出方式返回不同的回复。但由于某种原因,$request->header->get( 'accept' ) 或 $request->getRequestFormat() 总是返回 HTML(或分别为“text/html”),尽管浏览器根据 Firebug 发送了不同的内容,并且 Wireshark 也确认原始请求确实不同。
基本上我的代码是这样的:
public function someControllerAction() {
$request = $this->getRequest();
if( $request->isXmlHttpRequest() && $request->getRequestFormat() == 'html' ) {
// (1) Return some HTML fragment that is used to update the DOM at client side
} else if( $request->isXmlHttpRequest() && $request->getRequestFormat() == 'json' ) {
// (2) Return a JSON object
} else if( $request->getRequestFormat() == 'html' ) {
// (3) Return a full HTML page from a TWIG template
}
// In all other cases, return a 406 error
}
由于某种原因,只有 case (1) 或 case (3) 被执行,因为请求的格式总是等于 HTML。我已经试过了
die( var_dump( $request->header ) )
查看 Symfony 认为的标头是什么,我注意到标头中的其他一些细微变化与 Wireshark 所说的相比。例如,一切都转换为小写。
目前我最好的猜测是,在 Symfony 核心深处的某个地方似乎发生了某种规范化,它也将 Accept-Header-Field 替换为默认的“text/html”。有趣的是,X-Requested-With-Field 并没有被规范化,而是仍然存在。
如果请求对象返回实际的 (true) 标头字段,我需要进行哪些更改?
更新:结果
die( var_dump( getallheaders() ) );
也显示了错误的标题。 Symfony 是否会操作原生 PHP 变量,或者这是否表明 Apache 在将 accept 标头传递给 PHP 处理程序之前对其进行了处理?
【问题讨论】:
标签: symfony http-headers xmlhttprequest