1
1
.. index ::
2
2
single: Event Dispatcher
3
3
4
- How to setup before and after filters
4
+ How to setup before and after Filters
5
5
=====================================
6
6
7
- It is quite common in web applications development to need some logic to be executed just before
8
- or just after our controller actions acting as filters or hooks.
7
+ It is quite common in web application development to need some logic to be
8
+ executed just before or just after your controller actions acting as filters
9
+ or hooks.
9
10
10
- In Symfony1, this was achieved with the preExecute and postExecute methods, most major frameworks have similar
11
- methods but there is no such thing in Symfony2. Good news is that there is a much better way to interfere our
12
- Request -> Response process with the EventDispatcher component.
11
+ In Symfony1, this was achieved with the preExecute and postExecute methods,
12
+ most major frameworks have similar methods but there is no such thing in Symfony2.
13
+ The good news is that there is a much better way to interfere the
14
+ Request -> Response process using the EventDispatcher component.
13
15
14
- Token validation example
15
- ========================
16
+ Token validation Example
17
+ ------------------------
16
18
17
- Imagine that we need to develop an API where some controllers are public but some others are restricted
18
- to one or some clients. For this private features, we provide a token to our clients to identify themselves.
19
+ Imagine that you need to develop an API where some controllers are public
20
+ but some others are restricted to one or some clients. For these private features,
21
+ you might provide a token to your clients to identify themselves.
19
22
20
- So, before executing our controller action, we need to check if the action is restricted or not.
21
- And if it is restricted, we need to validate the provided token.
23
+ So, before executing your controller action, you need to check if the action
24
+ is restricted or not. And if it is restricted, you need to validate the provided
25
+ token.
22
26
23
27
.. note ::
24
28
25
- Please note that for simplicity in the recipe, tokens will be defined in config
26
- and neither database setup nor authentication provider via Security component will be used
29
+ Please note that for simplicity in the recipe, tokens will be defined
30
+ in config and neither database setup nor authentication provider via
31
+ the Security component will be used.
27
32
28
33
Creating a before filter with a controller.request event
29
- ========================================================
34
+ --------------------------------------------------------
30
35
31
36
Basic Setup
32
- -----------
37
+ ~~~~~~~~~~~
33
38
34
- We can add basic tokens configuration using config.yml and parameters key
39
+ You can add basic token configuration using `` config.yml `` and the parameters key:
35
40
36
41
.. configuration-block ::
37
42
@@ -61,15 +66,15 @@ We can add basic tokens configuration using config.yml and parameters key
61
66
'client2' => 'pass2'
62
67
));
63
68
64
- Tag controllers to be checked
69
+ Tag Controllers to be checked
65
70
-----------------------------
66
71
67
- A kernel.controller listener gets executed at every request, so we need some way to identify
68
- if the controller that matches the request needs a token validation.
69
-
70
- A clean and easy way is to create an empty interface and make the controllers implement it
72
+ A ``kernel.controller `` listener gets notified on every request, right before
73
+ the controller is executed. First, you need some way to identify if the controller
74
+ that matches the request needs token validation.
71
75
72
- .. code-block :: php
76
+ A clean and easy way is to create an empty interface and make the controllers
77
+ implement it::
73
78
74
79
namespace Acme\DemoBundle\Controller;
75
80
@@ -78,6 +83,8 @@ A clean and easy way is to create an empty interface and make the controllers im
78
83
// Nothing here
79
84
}
80
85
86
+ A controller that implements this interface simply looks like this::
87
+
81
88
class FooController implements TokenAuthenticatedController
82
89
{
83
90
// Your actions that need authentication
@@ -86,7 +93,9 @@ A clean and easy way is to create an empty interface and make the controllers im
86
93
Creating an Event Listener
87
94
--------------------------
88
95
89
- .. code-block :: php
96
+ Next, you'll need to create an event listener, which will hold the logic
97
+ that you want executed before your controllers. If you're not familiar with
98
+ event listeners, you can learn more about them at :doc: `/cookbook/service_container/event_listener `::
90
99
91
100
namespace Acme\DemoBundle\EventListener;
92
101
@@ -124,9 +133,13 @@ Creating an Event Listener
124
133
}
125
134
}
126
135
127
- Registering the listener
136
+ Registering the Listener
128
137
------------------------
129
138
139
+ Finally, register your listener as a service and tag it as an event listener.
140
+ By listening on ``kernel.controller ``, you're telling Symfony that you want
141
+ your listener to be called just before any controller is executed:
142
+
130
143
.. configuration-block ::
131
144
132
145
.. code-block :: yaml
0 commit comments