|
| 1 | +# Events |
| 2 | + |
| 3 | +DotKernel's controller package supports events and those events can be of 2 types: global events (middleware-like) or manually dispatch events. |
| 4 | + |
| 5 | +## Getting started |
| 6 | + |
| 7 | +- Every event listener that is triggered from a controller |
| 8 | +needs to implement `Dot\Controller\Event\ControllerEventListenerInterface` which actually extends `Laminas\EventManager\ListenerAggregateInterface`. |
| 9 | +- You can add the trait `Dot\Controller\Event\ControllerEventListenerTrait` to override the method of the interface. |
| 10 | +- Every event listener needs to be registered under the `['dot_controller']['event_listenenrs]` key in the ConfigProvider, and every key must be the class that you want to attach the events |
| 11 | + |
| 12 | +## Usage |
| 13 | + |
| 14 | +The events in a controller can be done in 2 different ways, a global way where an event is attached automatically to all the controllers action and works in the same way the middlewares works |
| 15 | +or a manually dispatchable way, where you can define to which controller the events is attached, and you can trigger the event where you want. |
| 16 | + |
| 17 | +For our example we have a UserController with some methods in it |
| 18 | + |
| 19 | +```php |
| 20 | +use DotKernel\DotController\AbstractActionController; |
| 21 | + |
| 22 | +class UserController extends AbstractActionController |
| 23 | +{ |
| 24 | + public function indexAction() |
| 25 | + { |
| 26 | + //... |
| 27 | + } |
| 28 | + |
| 29 | + public function registerAction() |
| 30 | + { |
| 31 | + //... |
| 32 | + } |
| 33 | + |
| 34 | + // post method for updating the user |
| 35 | + public function updateAction() |
| 36 | + { |
| 37 | + |
| 38 | + } |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +### Example 1 - Global way |
| 43 | + |
| 44 | +First we will create the event listener |
| 45 | + |
| 46 | +```php |
| 47 | +use Dot\Controller\Event\ControllerEvent; |
| 48 | +use Dot\Controller\Event\ControllerEventListenerInterface; |
| 49 | +use Dot\Controller\Event\ControllerEventListenerTrait; |
| 50 | + |
| 51 | +// for the logger we assume you will use your own logger and inject it |
| 52 | + |
| 53 | +class UserUpdatedListener implements ControllerEventListenerInterface |
| 54 | +{ |
| 55 | + use ControllerEventListenerTrait; |
| 56 | + |
| 57 | + public function onBeforeDispatch(ControllerEvent $event): void |
| 58 | + { |
| 59 | + $this->logger->info('on before dispatch'); |
| 60 | + } |
| 61 | + |
| 62 | + public function onAfterDispatch(ControllerEvent $event): void |
| 63 | + { |
| 64 | + $this->logger->info('on after dispatch'); |
| 65 | + } |
| 66 | + |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +We register the event listener in the configuration key |
| 71 | + |
| 72 | +```php |
| 73 | +'dot_controller' => [ |
| 74 | + 'event_listeners' => [ |
| 75 | + AccountController::class => [ |
| 76 | + UserUpdatedListener::class, |
| 77 | + ] |
| 78 | + ] |
| 79 | +] |
| 80 | +``` |
| 81 | + |
| 82 | +As you can assume, `onBeforeDispatch` is triggered right before the controller is dispatched, and `onAfterDispatch` right |
| 83 | +after the controller is dispatched. |
| 84 | + |
| 85 | +With this it doesn't matter what action is accessed, the event it will run before and after the action. |
| 86 | + |
| 87 | +In addition, you can make use of the `event` variable to access information about the event. |
| 88 | + |
| 89 | +For example: |
| 90 | + |
| 91 | +```php |
| 92 | +// UserUpdatedListener |
| 93 | +public function onAfterDispatch(ControllerEvent $e): void |
| 94 | + { |
| 95 | + $method = $e->getTarget()->getRequest()->getMethod(); |
| 96 | + $action = $e->getParams()['method']; |
| 97 | + if ($method == 'POST' && $action == 'updateAction') { |
| 98 | + $this->logger->info('this will trigger '); |
| 99 | + |
| 100 | + } |
| 101 | + } |
| 102 | +``` |
| 103 | + |
| 104 | +So every time the `updateAction` is accessed and the method is post, |
| 105 | +right after the action is dispatched, we can log that the user was updated. |
| 106 | + |
| 107 | +We can use the `onBeforeDispatch` in the same way, to log right before the user is updated. |
| 108 | + |
| 109 | +### Example 2 - Manually triggered way |
| 110 | + |
| 111 | +```php |
| 112 | +use Dot\Controller\Event\ControllerEvent; |
| 113 | +use Dot\Controller\Event\ControllerEventListenerInterface; |
| 114 | +use Dot\Controller\Event\ControllerEventListenerTrait; |
| 115 | + |
| 116 | +// for the logger we assume you will use your own logger and inject it |
| 117 | + |
| 118 | +class UserUpdatedListener implements ControllerEventListenerInterface |
| 119 | +{ |
| 120 | + use ControllerEventListenerTrait; |
| 121 | + |
| 122 | + public function attach(EventManagerInterface $events, $priority = 1): void |
| 123 | + { |
| 124 | + $this->listeners[] = $events->attach( |
| 125 | + 'user.profile.update', |
| 126 | + [$this, 'userProfileUpdated'], |
| 127 | + $priority |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + public function userProfileUpdated(ControllerEvent $event): void |
| 132 | + { |
| 133 | + $this->logger->info('User profile updated'); |
| 134 | + } |
| 135 | + |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +The `attach` method is from the `ListenerAggregateInterface` which `ControllerEventListenerTrait` |
| 140 | +already is overriding it so can be used in a global way with `onBeforeDispatch` and `onAfterDispatch` |
| 141 | +methods, but we can make our custom event and bind it to our method. |
| 142 | + |
| 143 | +In this case we create attach an event called `user.profile.update` and bind it to the `userProfileUpdated` method. |
| 144 | + |
| 145 | +Next we need to register the event |
| 146 | + |
| 147 | +```php |
| 148 | +'dot_controller' => [ |
| 149 | + 'event_listeners' => [ |
| 150 | + AccountController::class => [ |
| 151 | + 'user.profile.update' => UserUpdatedListener::class |
| 152 | + ] |
| 153 | + ] |
| 154 | +] |
| 155 | +``` |
| 156 | + |
| 157 | +Now you can manually trigger the event from the controller using build in `dispatchEvent` method. |
| 158 | + |
| 159 | +```php |
| 160 | +// UserController |
| 161 | +// post method for updating the user |
| 162 | +public function updateAction() |
| 163 | +{ |
| 164 | + // logic |
| 165 | + $this->dispatchEvent('user.profile.update', ['user' => $user]); |
| 166 | + |
| 167 | +} |
| 168 | +``` |
| 169 | + |
| 170 | +As you can see we attach the `user` key to the parameters, so we can actually access it. |
| 171 | + |
| 172 | +```php |
| 173 | + public function userProfileUpdated(ControllerEvent $event): void |
| 174 | + { |
| 175 | + $user = $event->getParams()['user']; |
| 176 | + $this->logger->info('User profile updated', $user->toArray()); |
| 177 | + } |
| 178 | +``` |
0 commit comments