Skip to content

Commit 4e85d2b

Browse files
committed
Minor tweaks to PR #1461:
* Shortened many lines slightly so that they break after the first word that crosses the 72nd character * Corrected the underlines used for the headers * Adding the map and index entries for the new cookbook
1 parent 38949cd commit 4e85d2b

File tree

3 files changed

+40
-25
lines changed

3 files changed

+40
-25
lines changed

cookbook/event_dispatcher/before_after_filters.rst

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,42 @@
11
.. index::
22
single: Event Dispatcher
33

4-
How to setup before and after filters
4+
How to setup before and after Filters
55
=====================================
66

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.
910

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.
1315

14-
Token validation example
15-
========================
16+
Token validation Example
17+
------------------------
1618

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.
1922

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.
2226

2327
.. note::
2428

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.
2732

2833
Creating a before filter with a controller.request event
29-
========================================================
34+
--------------------------------------------------------
3035

3136
Basic Setup
32-
-----------
37+
~~~~~~~~~~~
3338

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:
3540

3641
.. configuration-block::
3742

@@ -61,15 +66,15 @@ We can add basic tokens configuration using config.yml and parameters key
6166
'client2' => 'pass2'
6267
));
6368
64-
Tag controllers to be checked
69+
Tag Controllers to be checked
6570
-----------------------------
6671

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.
7175

72-
.. code-block:: php
76+
A clean and easy way is to create an empty interface and make the controllers
77+
implement it::
7378

7479
namespace Acme\DemoBundle\Controller;
7580

@@ -78,6 +83,8 @@ A clean and easy way is to create an empty interface and make the controllers im
7883
// Nothing here
7984
}
8085

86+
A controller that implements this interface simply looks like this::
87+
8188
class FooController implements TokenAuthenticatedController
8289
{
8390
// 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
8693
Creating an Event Listener
8794
--------------------------
8895

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`::
9099

91100
namespace Acme\DemoBundle\EventListener;
92101

@@ -124,9 +133,13 @@ Creating an Event Listener
124133
}
125134
}
126135

127-
Registering the listener
136+
Registering the Listener
128137
------------------------
129138

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+
130143
.. configuration-block::
131144

132145
.. code-block:: yaml

cookbook/event_dispatcher/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Event Dispatcher
44
.. toctree::
55
:maxdepth: 2
66

7+
before_after_filters
78
class_extension
89
method_behavior
910

cookbook/map.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151

5252
* :doc:`/cookbook/event_dispatcher/index`
5353

54+
* :doc:`/cookbook/event_dispatcher/before_after_filters`
5455
* :doc:`/cookbook/event_dispatcher/class_extension`
5556
* :doc:`/cookbook/event_dispatcher/method_behavior`
5657
* (service container) :doc:`/cookbook/service_container/event_listener`

0 commit comments

Comments
 (0)