Skip to content

Commit cf01193

Browse files
committed
simplified the 'How to register a new Request Format and Mime Type' recipe
1 parent 0e16cfc commit cf01193

File tree

1 file changed

+34
-98
lines changed

1 file changed

+34
-98
lines changed

cookbook/request/mime_type.rst

Lines changed: 34 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -15,139 +15,75 @@ object. Internally, Symfony contains a map of the most common formats (e.g.
1515
easily be added. This document will show how you can add the ``jsonp`` format
1616
and corresponding MIME type.
1717

18-
.. index::
19-
single: Bundles; Extension
20-
single: Dependency Injection, Extension
21-
2218
Create an ``onCoreRequest`` Listener
2319
------------------------------------
2420

25-
The key to defining a new MIME type, or making any other global change to
26-
the ``Request`` object, is to create a new class that will "listen" to the
27-
``onCoreRequest`` event thrown by the Symfony kernel. The ``onCoreRequest``
28-
event is thrown early in Symfony's bootstrapping process. By listening to
29-
this event, you can make any modifications needed to the request object.
30-
31-
To define a new MIME type, create a new configuration extension class that
32-
loads a service configuration file (e.g. ``services.xml``). A configuration
33-
extension class is one useful way to modify configuration from inside a
34-
bundle (see :ref:`service-container-extension-configuration` for more information).
35-
36-
.. note::
37-
38-
This guide places all of the code inside the ``Acme\DemoBundle`` namespace.
39-
Of course, the code can live inside any bundle in your project. Be sure
40-
to update the paths and namespaces accordingly.
21+
The key to defining a new MIME type is to create a class that will "listen" to
22+
the ``onCoreRequest`` event dispatched by the Symfony kernel. The
23+
``onCoreRequest`` event is dispatched early in Symfony's request handling
24+
process and allows you to modify the request object.
4125

42-
.. code-block:: php
26+
Create the following class, replacing the path with a path to a bundle in your
27+
project::
4328

44-
// src/Acme/DemoBundle/DependencyInjection/AcmeDemoExtension.php
45-
namespace Acme\DemoBundle\DependencyInjection;
29+
// src/Acme/DemoBundle/RequestListener.php
30+
namespace Acme\DemoBundle;
4631

47-
use Symfony\Component\DependencyInjection\ContainerBuilder;
48-
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
49-
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
50-
use Symfony\Component\Config\FileLocator;
32+
use Symfony\Component\HttpKernel\HttpKernelInterface;
33+
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
5134

52-
class AcmeDemoExtension extends Extension
35+
class RequestListener
5336
{
54-
public function load(array $configs, ContainerBuilder $container)
55-
{
56-
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
57-
$loader->load('services.xml');
58-
}
59-
60-
public function getAlias()
37+
public function onCoreRequest(GetResponseEvent $event)
6138
{
62-
return 'acme_demo';
39+
$event->getRequest()->setFormat('jsonp', 'application/javascript');
6340
}
6441
}
6542

66-
.. tip::
43+
Registering you Listener
44+
------------------------
6745

68-
If you're more comfortable defining configuration using either YAML
69-
or PHP, that's no problem. Simply change the ``XmlFileLoader`` namespace
70-
and class declaration to ``YamlFileLoader`` or ``PhpFileLoader`` and
71-
then load either ``services.yml`` or ``services.php``. You'll see how
72-
each configuration file would look next.
73-
74-
Now that the ``services.xml`` file is being loaded, you can use it to do the
75-
following:
76-
77-
* Define a service that will add the request format to the ``Request`` object;
78-
79-
* Tag the service correctly so that it is notified when the ``onCoreRequest``
80-
event is thrown.
46+
As for any other listener, you need to add it in one of your configuration
47+
file and register it as a listener by adding the ``kernel.listener`` tag:
8148

8249
.. configuration-block::
8350

8451
.. code-block:: xml
8552
86-
<!-- src/Acme/DemoBundle/Resources/config/services.xml -->
53+
<!-- app/config/config.xml -->
8754
<?xml version="1.0" ?>
8855
89-
<container xmlns="http://symfony.com/schema/dic/services"
90-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
91-
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
56+
<container xmlns="http://symfony.com/schema/dic/services"
57+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
58+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
9259
9360
<service id="acme.demobundle.listener.request" class="Acme\DemoBundle\RequestListener">
9461
<tag name="kernel.listener" event="onCoreRequest" />
9562
</service>
9663
97-
<!-- ... -->
98-
9964
</container>
10065
10166
.. code-block:: yaml
10267
103-
# src/Acme/DemoBundle/Resources/config/services.yml
104-
acme.demobundle.listener.request:
105-
class: Acme\DemoBundle\RequestListener
106-
tags:
107-
- { name: kernel.listener event: onCoreRequest }
68+
# app/config/config.yml
69+
services:
70+
acme.demobundle.listener.request:
71+
class: Acme\DemoBundle\RequestListener
72+
tags:
73+
- { name: kernel.listener event: onCoreRequest }
10874
10975
.. code-block:: php
110-
111-
# src/Acme/DemoBundle/Resources/config/services.php
112-
76+
77+
# app/config/config.php
11378
$definition = new Definition('Acme\DemoBundle\RequestListener');
11479
$definition->addTag('kernel.listener', array('event' => 'onCoreRequest'));
11580
$container->setDefinition('acme.demobundle.listener.request', $definition);
11681
11782
At this point, the ``acme.demobundle.listener.request`` service has been
118-
configured and will be notified when the Symfony kernel throws the ``onCoreRequest``
119-
event.
120-
121-
Create the Request Listener Class
122-
---------------------------------
123-
124-
All that's left now is to create the actual class that will modify the ``Request``
125-
object. Create the following class, replacing the path with a path to a bundle in
126-
your project:
127-
128-
.. code-block:: php
129-
130-
// src/Acme/DemoBundle/RequestListener.php
131-
132-
namespace Acme\DemoBundle;
133-
134-
use Symfony\Component\HttpKernel\HttpKernelInterface;
135-
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
136-
137-
class RequestListener
138-
{
139-
public function onCoreRequest(GetResponseEvent $event)
140-
{
141-
$request = $event->getRequest();
142-
$request->setFormat('jsonp', 'application/javascript');
143-
}
144-
}
83+
configured and will be notified when the Symfony kernel dispatches the
84+
``onCoreRequest`` event.
14585

146-
Looking Back
147-
------------
86+
.. tip::
14887

149-
Adding a new request format and mime type to Symfony is easy, and ultimately
150-
involves calling the ``setFormat`` method on the ``Request`` object. To
151-
be sure that the ``Request`` object is modified at a global level, you can
152-
listen to one of the Symfony kernel events (``onCoreRequest``), allowing
153-
you to modifying anything related to the ``Request`` object.
88+
You can also register the listener in a configuration extension class (see
89+
:ref:`service-container-extension-configuration` for more information).

0 commit comments

Comments
 (0)