Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions en/appendices/5-1-migration-guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ Core
- The ``toString``, ``toInt``, ``toBool`` functions were added. They give you
a typesafe way to cast request data or other input and return ``null`` when conversion fails.
- ``pathCombine()`` was added to help build paths without worrying about duplicate and trailing slashes.
- A new ``events`` hook was added to the ``BaseApplication`` as well as the ``BasePlugin`` class. This hook
is the recommended way to register global event listeners for you application. See :ref:`Registering Listeners <registering-event-listeners>`

Database
--------
Expand Down
24 changes: 24 additions & 0 deletions en/core-libraries/events.rst
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,30 @@ As you can see in the above code, the ``on()`` function will accept instances
of the ``EventListener`` interface. Internally, the event manager will use
``implementedEvents()`` to attach the correct callbacks.

.. versionadded:: 5.1.0
The ``events`` hook was added to the ``BaseApplication`` as well as the ``BasePlugin`` class

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

namespace App;

use App\Event\UserStatistic;
use Cake\Event\EventManagerInterface;
use Cake\Http\BaseApplication;

class Application extends BaseApplication
{
// The rest of your Application class

public function events(EventManagerInterface $eventManager): EventManagerInterface
{
$statistics = new UserStatistic();
$eventManager->on($statistics);

return $eventManager;
}
}

Registering Anonymous Listeners
-------------------------------

Expand Down