How to Register a new Request Format and Mime Type

Version: 3.1
Edit this page

Warning: You are browsing the documentation for Symfony 3.1, which is no longer maintained.

Read the updated version of this page for Symfony 5.3 (the current stable version).

How to Register a new Request Format and Mime Type

Every Request has a "format" (e.g. html, json), which is used to determine what type of content to return in the Response. In fact, the request format, accessible via getRequestFormat(), is used to set the MIME type of the Content-Type header on the Response object. Internally, Symfony contains a map of the most common formats (e.g. html, json) and their associated MIME types (e.g. text/html, application/json). Of course, additional format-MIME type entries can easily be added. This document will show how you can add the jsonp format and corresponding MIME type.

Configure your New Format

The FrameworkBundle registers a subscriber that will add formats to incoming requests.

All you have to do is to configure the jsonp format:

  • YAML
  • XML
  • PHP
1
2
3
4
5
# app/config/config.yml
framework:
    request:
        formats:
            jsonp: 'application/javascript'

Tip

You can also associate multiple mime types to a format, but please note that the preferred one must be the first as it will be used as the content type:

  • YAML
  • XML
  • PHP
1
2
3
4
5
# app/config/config.yml
framework:
    request:
        formats:
            csv: ['text/csv', 'text/plain']
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.