In my controller function I need to return different replies based on the accepted content type and how the request was made. But for some reason $request->header->get( 'accept' ) or $request->getRequestFormat() always returns HTML (or "text/html" respectively) although the browser has sent something different according to Firebug and Wireshark also confirms that the original request was really different.
Basically my code looks like this:
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
}
For some reason, only case (1) or case (3) are executed, because the requested format always equals HTML. I have already tried
die( var_dump( $request->header ) )
to have a look at what Symfony thinks the header was and I noticed some other minor changes in the header compared to what Wireshark tells. For example,everything is converted to lowercase.
At the moment my best guess is that somewhere in the depth of the Symfony core some kind of normalization seems to occur which also replaces the Accept-Header-Field by a default "text/html". Interestingly, the X-Requested-With-Field is not normalized away but survives.
What do I have to change that the request object returns the actual (true) header fields?
UPDATE: The result of
die( var_dump( getallheaders() ) );
shows the wrong header, too. Does Symfony manipulate the native PHP variables or does this indicate that Apache twiddles with the accept header before passing it to the PHP handler?