@@ -15,139 +15,75 @@ 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
- .. index ::
19
- single: Bundles; Extension
20
- single: Dependency Injection, Extension
21
-
22
18
Create an ``onCoreRequest `` Listener
23
19
------------------------------------
24
20
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.
41
25
42
- .. code-block :: php
26
+ Create the following class, replacing the path with a path to a bundle in your
27
+ project::
43
28
44
- // src/Acme/DemoBundle/DependencyInjection/AcmeDemoExtension .php
45
- namespace Acme\DemoBundle\DependencyInjection ;
29
+ // src/Acme/DemoBundle/RequestListener .php
30
+ namespace Acme\DemoBundle;
46
31
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;
51
34
52
- class AcmeDemoExtension extends Extension
35
+ class RequestListener
53
36
{
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)
61
38
{
62
- return 'acme_demo' ;
39
+ $event->getRequest()->setFormat('jsonp', 'application/javascript') ;
63
40
}
64
41
}
65
42
66
- .. tip ::
43
+ Registering you Listener
44
+ ------------------------
67
45
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:
81
48
82
49
.. configuration-block ::
83
50
84
51
.. code-block :: xml
85
52
86
- <!-- src/Acme/DemoBundle/Resources/ config/services .xml -->
53
+ <!-- app/ config/config .xml -->
87
54
<?xml version =" 1.0" ?>
88
55
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" >
92
59
93
60
<service id =" acme.demobundle.listener.request" class =" Acme\DemoBundle\RequestListener" >
94
61
<tag name =" kernel.listener" event =" onCoreRequest" />
95
62
</service >
96
63
97
- <!-- ... -->
98
-
99
64
</container >
100
65
101
66
.. code-block :: yaml
102
67
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 }
108
74
109
75
.. code-block :: php
110
-
111
- # src/Acme/DemoBundle/Resources/config/services.php
112
-
76
+
77
+ # app/config/config.php
113
78
$definition = new Definition('Acme\DemoBundle\RequestListener');
114
79
$definition->addTag('kernel.listener', array('event' => 'onCoreRequest'));
115
80
$container->setDefinition('acme.demobundle.listener.request', $definition);
116
81
117
82
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.
145
85
146
- Looking Back
147
- ------------
86
+ .. tip ::
148
87
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