Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • SensioLabs Professional services to help you with Symfony
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by SensioLabs
  1. Home
  2. Documentation
  3. Cookbook
  4. Request
  5. How to Register a new Request Format and Mime Type
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud

Table of Contents

  • Create a kernel.request Listener
  • Registering your Listener

How to Register a new Request Format and Mime Type

Edit this page

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

Read the updated version of this page for Symfony 6.2 (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.

Create a kernel.request Listener

The key to defining a new MIME type is to create a class that will "listen" to the kernel.request event dispatched by the Symfony kernel. The kernel.request event is dispatched early in Symfony's request handling process and allows you to modify the request object.

Create the following class, replacing the path with a path to a bundle in your project:

1
2
3
4
5
6
7
8
9
10
11
12
13
// src/AppBundle/EventListener/RequestListener.php
namespace AppBundle\EventListener;

use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;

class RequestListener
{
    public function onKernelRequest(GetResponseEvent $event)
    {
        $event->getRequest()->setFormat('jsonp', 'application/javascript');
    }
}

Registering your Listener

As with any other listener, you need to add it in one of your configuration files and register it as a listener by adding the kernel.event_listener tag:

  • YAML
  • XML
  • PHP
1
2
3
4
5
6
# app/config/services.yml
services:
    app.listener.request:
        class: AppBundle\EventListener\RequestListener
        tags:
            - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!-- app/config/services.xml -->
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
    <services>
        <service id="app.listener.request"
            class="AppBundle\EventListener\RequestListener">
            <tag name="kernel.event_listener"
                event="kernel.request"
                method="onKernelRequest"
            />
        </service>
    </services>
</container>
1
2
3
4
5
6
7
# app/config/services.php
$definition = new Definition('AppBundle\EventListener\RequestListener');
$definition->addTag('kernel.event_listener', array(
    'event'  => 'kernel.request',
    'method' => 'onKernelRequest',
));
$container->setDefinition('app.listener.request', $definition);

At this point, the app.listener.request service has been configured and will be notified when the Symfony kernel dispatches the kernel.request event.

Tip

You can also register the listener in a configuration extension class (see Service Container for more information).

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    We stand with Ukraine.
    Version:
    Online Sylius certification, take it now!

    Online Sylius certification, take it now!

    Code consumes server resources. Blackfire tells you how

    Code consumes server resources. Blackfire tells you how

    Symfony footer

    ↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

    Avatar of Alexey Kopytko, a Symfony contributor

    Thanks Alexey Kopytko (@sanmai) for being a Symfony contributor

    9 commits • 172 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • Symfony at a Glance
      • Symfony Components
      • Case Studies
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • SymfonyConnect
      • Support
      • How to be Involved
      • Code of Conduct
      • Events & Meetups
      • Projects using Symfony
      • Downloads Stats
      • Contributors
      • Backers
    • Blog

      • Events & Meetups
      • A week of symfony
      • Case studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Documentation
      • Living on the edge
      • Releases
      • Security Advisories
      • SymfonyInsight
      • Twig
      • SensioLabs
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Deployed on

    Follow Symfony

    Search by Algolia