Skip to content

Commit bd0d2c8

Browse files
authored
Merge pull request #7905 from cakephp/5.next-events-hook
5.next: add docs related to new events hook
2 parents 8f9fce8 + eaebee7 commit bd0d2c8

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

en/appendices/5-1-migration-guide.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ Core
8888
- The ``toString``, ``toInt``, ``toBool`` functions were added. They give you
8989
a typesafe way to cast request data or other input and return ``null`` when conversion fails.
9090
- ``pathCombine()`` was added to help build paths without worrying about duplicate and trailing slashes.
91+
- A new ``events`` hook was added to the ``BaseApplication`` as well as the ``BasePlugin`` class. This hook
92+
is the recommended way to register global event listeners for you application. See :ref:`Registering Listeners <registering-event-listeners>`
9193

9294
Database
9395
--------

en/core-libraries/events.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,30 @@ As you can see in the above code, the ``on()`` function will accept instances
198198
of the ``EventListener`` interface. Internally, the event manager will use
199199
``implementedEvents()`` to attach the correct callbacks.
200200

201+
.. versionadded:: 5.1.0
202+
The ``events`` hook was added to the ``BaseApplication`` as well as the ``BasePlugin`` class
203+
204+
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::
205+
206+
namespace App;
207+
208+
use App\Event\UserStatistic;
209+
use Cake\Event\EventManagerInterface;
210+
use Cake\Http\BaseApplication;
211+
212+
class Application extends BaseApplication
213+
{
214+
// The rest of your Application class
215+
216+
public function events(EventManagerInterface $eventManager): EventManagerInterface
217+
{
218+
$statistics = new UserStatistic();
219+
$eventManager->on($statistics);
220+
221+
return $eventManager;
222+
}
223+
}
224+
201225
Registering Anonymous Listeners
202226
-------------------------------
203227

0 commit comments

Comments
 (0)