Gets a list of content types acceptable by the client browser.
public getAcceptableContentTypes ( ) : array | ||
return | array | List of content types in preferable order |
$request = Request::createFromGlobals(); $acceptableTypes = $request->getAcceptableContentTypes(); if (in_array('application/json', $acceptableTypes)) { // Send JSON response } elseif (in_array('text/html', $acceptableTypes)) { // Send HTML response } else { // Send plain text response }
$request = Request::createFromGlobals(); $acceptableTypes = $request->getAcceptableContentTypes(); if (!in_array('application/json', $acceptableTypes)) { throw new \Exception('Client does not accept JSON'); }In this example, we create a Request object from the global variables and then call getAcceptableContentTypes to get an array of acceptable content types. We then check if the client accepts JSON and throw an exception if they do not. Package Library: Symfony\Component\HttpFoundation is a package library that provides a set of classes for handling HTTP requests and responses in Symfony applications.
public getAcceptableContentTypes ( ) : array | ||
return | array | List of content types in preferable order |