Use the getFormat() method on the request object for content type negotiation, and fallback to html in the case of no Accept header.

8.0.x
Katherine Bailey 2012-04-18 22:06:17 -07:00 committed by Larry Garfield
parent 89335b2cd3
commit 223049a02c
1 changed files with 7 additions and 9 deletions

View File

@ -27,8 +27,6 @@ class ContentNegotiation {
* The request object from which to extract the content type. * The request object from which to extract the content type.
*/ */
public function getContentType(Request $request) { public function getContentType(Request $request) {
$acceptable_content_types = $request->getAcceptableContentTypes();
// AJAX iframe uploads need special handling, because they contain a json // AJAX iframe uploads need special handling, because they contain a json
// response wrapped in <textarea>. // response wrapped in <textarea>.
if ($request->get('ajax_iframe_upload', FALSE)) { if ($request->get('ajax_iframe_upload', FALSE)) {
@ -40,16 +38,16 @@ class ContentNegotiation {
return 'ajax'; return 'ajax';
} }
// JSON requests can be responded to using JsonResponse(). foreach ($request->getAcceptableContentTypes() as $mime_type) {
elseif (in_array('application/json', $acceptable_content_types)) { $format = $request->getFormat($mime_type);
return 'json'; if (!is_null($format)) {
return $format;
}
} }
// Do HTML last so that it always wins for */* formats. // Do HTML last so that it always wins.
elseif(in_array('text/html', $acceptable_content_types) || in_array('*/*', $acceptable_content_types)) {
return 'html'; return 'html';
} }
}
} }