You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/book/v4/usage.md
+14-16Lines changed: 14 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,19 +2,19 @@
2
2
3
3
This tutorial explores various examples of dot-event usages.
4
4
5
-
To start using events you will need the following things:
5
+
To start using events, you will need the following things:
6
6
7
7
- An **Event**
8
-
- One or more **listeners** that will be registered on the application config (``ConfigProvider``)
8
+
- One or more **listeners** that will be registered on the application config (`ConfigProvider`)
9
9
- An instance of **EventManager** from the **container** so you can trigger the events
10
10
11
-
The listeners needs to be registered in the `ConfigProvider`, under the `['dot-event']` key.
11
+
The listeners need to be registered in the `ConfigProvider`, under the `['dot-event']` key.
12
12
13
13
## Example
14
14
15
15
The below example will implement an event for update users.
16
16
17
-
Every event needs to extends`Dot\Event\Event`:
17
+
Every event needs to extend`Dot\Event\Event`:
18
18
19
19
```php
20
20
class UserEvent extends Event
@@ -31,7 +31,7 @@ class UserEvent extends Event
31
31
```
32
32
33
33
We use the concept of listener aggregates because with this approach in a single class we can listen to multiple events.
34
-
If you pay attention, in the above event we have 2 events `pre.update.user` and `post.update.user`.
34
+
If you pay attention, in the above event we have two events `pre.update.user` and `post.update.user`.
35
35
36
36
Every listener needs to extend `Dot\Event\ControllerEventListenerInterface`.
37
37
The interface defines two methods `attach()` and `detach()`.
@@ -110,10 +110,10 @@ class MyService
110
110
111
111
You can attach multiple listeners to the same event but with different logic.
112
112
113
-
All listeners are executed in the order in which they are attached. However, you can provide a priority value and you can influence the order of the execution.
113
+
All listeners are executed in the order in which they are attached. However, you can provide a priority value, and you can influence the order of the execution.
114
114
115
-
- Higher priority values execute earlier.
116
-
- Lower (negative) priority values execute later.
115
+
- Higher priority values execute earlier
116
+
- Lower (negative) priority values execute later
117
117
118
118
```php
119
119
class UserEventListener implements DotEventListenerInterface
@@ -136,12 +136,12 @@ class UserEventListener implements DotEventListenerInterface
136
136
}
137
137
```
138
138
139
-
As you can notice, we attach the same event name to listeners, so once we trigger the event both callback will run one after another.
140
-
But because we provide priority, the second attach will run first because has a higher priority.
139
+
As you can notice, we attach the same event name to listeners, so once we trigger the event, both callbacks will run one after another.
140
+
But because we provide priority, the second attachment will run first because it has a higher priority.
141
141
142
142
## Short-circuiting the execution
143
143
144
-
Sometimes you have more listeners to an event, and you may want to stop the execution of the event if something is wrong in one of the listeners.
144
+
Sometimes you have more listeners to an event, and you may want to stop the execution of the event if something is wrong with one of the listeners.
145
145
146
146
```php
147
147
@@ -152,29 +152,27 @@ Sometimes you have more listeners to an event, and you may want to stop the exec
152
152
153
153
public function onUserPostUpdateSecond(UserEvent $event)
154
154
{
155
-
156
155
// this will not execute
157
-
158
156
}
159
157
```
160
158
161
159
## Returns
162
160
163
-
You can return whatever you want in a listener callback. All events trigger returns an instance of `Laminas\EventManager\ResponseCollection` so you can have information if the event has stopped, or if event returned some expected object.
161
+
You can return whatever you want in a listener callback.
162
+
All events trigger return an instance of `Laminas\EventManager\ResponseCollection` so you can have information if the event has stopped, or if event returned some expected object.
0 commit comments