@@ -9,7 +9,7 @@ or just after our controller actions acting as filters or hooks.
9
9
10
10
In Symfony1, this was achieved with the preExecute and postExecute methods, most major frameworks have similar
11
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 EventListener component.
12
+ Request -> Response process with the EventDispatcher component.
13
13
14
14
Token validation example
15
15
========================
@@ -22,8 +22,8 @@ And if it is restricted, we need to validate the provided token.
22
22
23
23
.. note ::
24
24
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
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
27
27
28
28
Creating a before filter with a controller.request event
29
29
========================================================
@@ -43,6 +43,23 @@ We can add basic tokens configuration using config.yml and parameters key
43
43
client1 : pass1
44
44
client2 : pass2
45
45
46
+ .. code-block :: xml
47
+
48
+ <!-- app/config/config.xml -->
49
+ <parameters >
50
+ <parameter key =" tokens" type =" collection" >
51
+ <parameter key =" client1" >pass1</parameter >
52
+ <parameter key =" client2" >pass2</parameter >
53
+ </parameter >
54
+ </parameters >
55
+
56
+ .. code-block :: php
57
+
58
+ // app/config/config.php
59
+ $container->setParameter('tokens', array(
60
+ 'client1' => 'pass1',
61
+ 'client2' => 'pass2'
62
+ ));
46
63
47
64
Tag controllers to be checked
48
65
-----------------------------
@@ -58,12 +75,12 @@ A clean and easy way is to create an empty interface and make the controllers im
58
75
59
76
interface TokenAuthenticatedController
60
77
{
61
- // Nothing here
78
+ // Nothing here
62
79
}
63
80
64
81
class FooController implements TokenAuthenticatedController
65
82
{
66
- // Your actions that need authentication
83
+ // Your actions that need authentication
67
84
}
68
85
69
86
Creating an Event Listener
@@ -92,7 +109,9 @@ Creating an Event Listener
92
109
* $controller passed can be either a class or a Closure. This is not usual in Symfony2 but it may happen.
93
110
* If it is a class, it comes in array format
94
111
*/
95
- if (!is_array($controller)) return;
112
+ if (!is_array($controller)) {
113
+ return;
114
+ }
96
115
97
116
if($controller[0] instanceof TokenAuthenticatedController) {
98
117
$token = $event->getRequest()->get('token');
@@ -103,8 +122,8 @@ Creating an Event Listener
103
122
}
104
123
}
105
124
106
- Tagging the EventListener
107
- -------------------------
125
+ Registering the listener
126
+ ------------------------
108
127
109
128
.. configuration-block ::
110
129
0 commit comments