@@ -15,81 +15,107 @@ object. Internally, Symfony contains a map of the most common formats (e.g.
15
15
easily be added. This document will show how you can add the ``jsonp `` format
16
16
and corresponding MIME type.
17
17
18
- Create a `` kernel.request `` Listener
19
- -------------------------------------
18
+ .. versionadded :: 2.5
19
+ The possibility to configure request formats was introduced in Symfony 2.5.
20
20
21
- The key to defining a new MIME type is to create a class that will "listen" to
22
- the ``kernel.request `` event dispatched by the Symfony kernel. The
23
- ``kernel.request `` event is dispatched early in Symfony's request handling
24
- process and allows you to modify the request object.
25
-
26
- Create the following class, replacing the path with a path to a bundle in your
27
- project::
28
-
29
- // src/Acme/DemoBundle/RequestListener.php
30
- namespace Acme\DemoBundle;
31
-
32
- use Symfony\Component\HttpKernel\HttpKernelInterface;
33
- use Symfony\Component\HttpKernel\Event\GetResponseEvent;
34
-
35
- class RequestListener
36
- {
37
- public function onKernelRequest(GetResponseEvent $event)
38
- {
39
- $event->getRequest()->setFormat('jsonp', 'application/javascript');
40
- }
41
- }
42
-
43
- Registering your Listener
21
+ Configure your New Format
44
22
-------------------------
45
23
46
- As with any other listener, you need to add it in one of your configuration
47
- files and register it as a listener by adding the ``kernel.event_listener `` tag:
24
+ The FrameworkBundle registers a subscriber that will add formats to incomming requests.
25
+
26
+ All you have to do is to configure the ``jsonp `` format:
48
27
49
28
.. configuration-block ::
50
29
51
30
.. code-block :: yaml
52
31
53
32
# app/config/config.yml
54
- services :
55
- acme.demobundle.listener.request :
56
- class : Acme\DemoBundle\RequestListener
57
- tags :
58
- - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
33
+ framework :
34
+ request :
35
+ formats :
36
+ jsonp : ' application/javascript'
59
37
60
38
.. code-block :: xml
61
39
62
40
<!-- app/config/config.xml -->
63
- <?xml version =" 1.0" ?>
41
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
42
+
64
43
<container xmlns =" http://symfony.com/schema/dic/services"
65
44
xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
66
- xsi : schemaLocation =" http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd" >
67
- <services >
68
- <service id =" acme.demobundle.listener.request"
69
- class =" Acme\DemoBundle\RequestListener" >
70
- <tag name =" kernel.event_listener"
71
- event =" kernel.request"
72
- method =" onKernelRequest"
73
- />
74
- </service >
75
- </services >
45
+ xmlns : framework =" http://symfony.com/schema/dic/symfony"
46
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
47
+ http://symfony.com/schema/dic/services/services-1.0.xsd
48
+ http://symfony.com/schema/dic/symfony
49
+ http://symfony.com/schema/dic/symfony/symfony-1.0.xsd"
50
+ >
51
+ <framework : config >
52
+ <framework : request >
53
+ <framework : format name =" jsonp" >
54
+ <framework : mime-type >application/javascript</framework : mime-type >
55
+ </framework : format >
56
+ </framework : request >
57
+ </framework : config >
76
58
</container >
77
59
78
60
.. code-block :: php
79
61
80
- # app/config/config.php
81
- $definition = new Definition('Acme\DemoBundle\RequestListener');
82
- $definition->addTag('kernel.event_listener', array(
83
- 'event' => 'kernel.request',
84
- 'method' => 'onKernelRequest',
62
+ // app/config/config.php
63
+ $container->loadFromExtension('framework', array(
64
+ 'request' => array(
65
+ 'formats' => array(
66
+ 'jsonp' => 'application/javascript',
67
+ ),
68
+ ),
85
69
));
86
- $container->setDefinition('acme.demobundle.listener.request', $definition);
87
-
88
- At this point, the ``acme.demobundle.listener.request `` service has been
89
- configured and will be notified when the Symfony kernel dispatches the
90
- ``kernel.request `` event.
91
70
92
71
.. tip ::
93
72
94
- You can also register the listener in a configuration extension class (see
95
- :ref: `service-container-extension-configuration ` for more information).
73
+ You can also associate multiple mime types to a format, but please note that
74
+ the preferred one must be the first as it will be used as the content type:
75
+
76
+ .. configuration-block ::
77
+
78
+ .. code-block :: yaml
79
+
80
+ # app/config/config.yml
81
+ framework :
82
+ request :
83
+ formats :
84
+ csv : ['text/csv', 'text/plain']
85
+
86
+ .. code-block :: xml
87
+
88
+ <!-- app/config/config.xml -->
89
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
90
+
91
+ <container xmlns =" http://symfony.com/schema/dic/services"
92
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
93
+ xmlns : framework =" http://symfony.com/schema/dic/symfony"
94
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
95
+ http://symfony.com/schema/dic/services/services-1.0.xsd
96
+ http://symfony.com/schema/dic/symfony
97
+ http://symfony.com/schema/dic/symfony/symfony-1.0.xsd"
98
+ >
99
+ <framework : config >
100
+ <framework : request >
101
+ <framework : format name =" csv" >
102
+ <framework : mime-type >text/csv</framework : mime-type >
103
+ <framework : mime-type >text/plain</framework : mime-type >
104
+ </framework : format >
105
+ </framework : request >
106
+ </framework : config >
107
+ </container >
108
+
109
+ .. code-block :: php
110
+
111
+ // app/config/config.php
112
+ $container->loadFromExtension('framework', array(
113
+ 'request' => array(
114
+ 'formats' => array(
115
+ 'jsonp' => array(
116
+ 'text/csv',
117
+ 'text/plain',
118
+ ),
119
+ ),
120
+ ),
121
+ ));
0 commit comments