Contributed by
Andrei Igna
in #26486.
The Accept HTTP header is added by browsers to their requests to advertise which content types they are able to understand. These content types are expressed as MIME types.
The HttpFoundation component provides several utilities in its Request
class to work with request formats and MIME types (such as getMimeType()
,
getFormat()
, getRequestFormat()
, getContentType()
,
getAcceptableContentTypes()
, etc.)
In Symfony 4.2 we added a new method called getAcceptableFormats()
which
returns a simplified list of formats accepted by the client based on the
Accept
header. This simple list is what most apps commonly need to decide
which format to serve:
1 2 3 4 5 6 7 8 9 | // $request is an instance of Symfony\Component\HttpFoundation\Request
// BEFORE: this was the best method to get the list of accepted content types
$request->getAcceptableContentTypes();
// returns ['text/html', 'application/xhtml+xml', 'application/xml', '*/*']
// AFTER: the list returned by the new method is easier to understand and work with
$request->getAcceptableFormats();
// returns ['html', 'xml']
|
What a Symfony developer should know about the framework: News, Jobs, Tweets, Events, Videos,...