Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Request.php

function Request::getPreferredFormat

Gets the preferred format for the response by inspecting, in the following order:

  • the request format set using setRequestFormat;
  • the values of the Accept HTTP header.

Note that if you use this method, you should send the "Vary: Accept" header in the response to prevent any issues with intermediary HTTP caches.

File

vendor/symfony/http-foundation/Request.php, line 1519

Class

Request
Request represents an HTTP request.

Namespace

Symfony\Component\HttpFoundation

Code

public function getPreferredFormat(?string $default = 'html') : ?string {
    if (!isset($this->preferredFormat) && null !== ($preferredFormat = $this->getRequestFormat(null))) {
        $this->preferredFormat = $preferredFormat;
    }
    if ($this->preferredFormat ?? null) {
        return $this->preferredFormat;
    }
    foreach ($this->getAcceptableContentTypes() as $mimeType) {
        if ($this->preferredFormat = $this->getFormat($mimeType)) {
            return $this->preferredFormat;
        }
    }
    return $default;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal