@@ -298,16 +298,86 @@ As you can see in the above code, the `on()` function will accept instances
298298of the ` EventListener ` interface. Internally, the event manager will use
299299` implementedEvents() ` to attach the correct callbacks.
300300
301- ::: info Added in version 5.1 .0
302- The ` events ` hook was added to the ` BaseApplication ` as well as the ` BasePlugin ` class
301+ ::: info Added in version 5.4 .0
302+ The ` eventListeners ` hook was added to ` BaseApplication ` and ` BasePlugin ` .
303303:::
304304
305- As of CakePHP 5.1 it is recommended to register event listeners by adding them via the ` events ` hook in your application or plugin class:
305+ As of CakePHP 5.4, applications and plugins can register listener classes with
306+ the ` eventListeners() ` hook. Listener classes are resolved through the
307+ application's dependency injection container before they are attached to the
308+ global event manager. This lets listeners declare constructor dependencies:
306309
307310``` php
308311namespace App;
309312
310313use App\Event\UserStatistic;
314+ use Cake\Http\BaseApplication;
315+
316+ class Application extends BaseApplication
317+ {
318+ // The rest of your Application class
319+
320+ /**
321+ * @return list<class-string <\Cake\Event\EventListenerInterface >>
322+ */
323+ public function eventListeners(): array
324+ {
325+ return [
326+ UserStatistic::class,
327+ ];
328+ }
329+ }
330+ ```
331+
332+ Plugins can define event listeners the same way in their plugin class:
333+
334+ ``` php
335+ namespace ContactManager;
336+
337+ use Cake\Core\BasePlugin;
338+ use ContactManager\Event\UserStatistic;
339+
340+ class ContactManagerPlugin extends BasePlugin
341+ {
342+ /**
343+ * @return list<class-string <\Cake\Event\EventListenerInterface >>
344+ */
345+ public function eventListeners(): array
346+ {
347+ return [
348+ UserStatistic::class,
349+ ];
350+ }
351+ }
352+ ```
353+
354+ If your listener has constructor dependencies, register the listener and its
355+ dependencies in ` Application::services() ` or ` Plugin::services() ` :
356+
357+ ``` php
358+ use App\Event\UserStatistic;
359+ use App\Service\StatisticsClient;
360+ use Cake\Core\ContainerInterface;
361+
362+ public function services(ContainerInterface $container): void
363+ {
364+ $container->addShared(StatisticsClient::class);
365+ $container->addShared(UserStatistic::class)
366+ ->addArgument(StatisticsClient::class);
367+ }
368+ ```
369+
370+ ::: info Added in version 5.1.0
371+ The ` events ` hook was added to the ` BaseApplication ` as well as the ` BasePlugin ` class.
372+ :::
373+
374+ Use the ` events() ` hook in your application or plugin class when you need
375+ imperative registration logic, or want to register anonymous functions:
376+
377+ ``` php
378+ namespace App;
379+
380+ use Cake\Event\EventInterface;
311381use Cake\Event\EventManagerInterface;
312382use Cake\Http\BaseApplication;
313383
@@ -317,8 +387,9 @@ class Application extends BaseApplication
317387
318388 public function events(EventManagerInterface $eventManager): EventManagerInterface
319389 {
320- $statistics = new UserStatistic();
321- $eventManager->on($statistics);
390+ $eventManager->on('Order.afterPlace', function (EventInterface $event): void {
391+ // Code to update statistics
392+ });
322393
323394 return $eventManager;
324395 }
0 commit comments