Help? how event system works (solved)

i follow and done all the cookbook steps but i can’t use event systems in cakephp 3.9.2
i tried in controller and in model but its doesn’t work!

is event system only work in plugins???

dose anyone work for them its been 2 weeks trying and am tired :slightly_frowning_face: please help me

i finally find a way after 2 weeks thank you

Can you please provide your working solution?
As I have similar questions it would be nice to know how its done.
Thanks!

//your new listener located in src\Event\BlogsListener

namespace App\Event;

use Cake\Event\EventListenerInterface;
use Cake\Log\Log

class BlogsListener implements EventListenerInterface {

    public function implementedEvents() {
        return [
            'Controller.Blogs.check' => 'check',
        ];
    }

    public function check($event) {
      Log::write('debug','my first event');
    }
}

//in your bootstrap link your listener
//first you must include your new listener to bootstrap
//you must include your listener class if not it dosen’t work

use Cake\Event\EventManager;
    use App\Event\BlogsListener;

$myevent = new BlogsListener();
EventManager::instance()->on($myevent);
Hope this help you brother !

Cheers for that :slight_smile:

Now I am guessing here, but I assume you have a /src/Controller/BlogController.php which has a public function check() along with its /templates/Blogs/check.php and your event triggers when they navigate to URL/block/check?

Sorry if that question is stating the obvious, I just want to be sure what I am looking at here!!

Also, for anyone else here who may know, is this the correct way to also do it in Cake 4?

Thanks again

no brother check is the event name

u can place events anywere in your controller i only use it in blogs with public function add() after saving an entity when i add new blog and save event trigger and listener call
like this

$event = new Event('Controller.Blogs.check', $this)
$this->geteventManager()->dispatch($event);

sorry for my broken english brother

To make events work you first must register the event handling logic with one of the event managers.

@Ermias shows an example of how that can be done. First he creates a class that holds the logic that will be executed when the event is triggered, then he makes sure that logic is known to the event system.

He performs that second step with this code in his bootstrap:

use Cake\Event\EventManager;
    use App\Event\BlogsListener;

$myevent = new BlogsListener();
EventManager::instance()->on($myevent);

When the EventManager receives the instance of the BlogsListener class, it will use the array returned by implementedEvents() to identify all the event handling methods and will register them by name; the keys of the array provides the event names, the values are the names of the methods that will run when the event is triggered.

Any class can be sent to the event manager as long as it has the implementedEvents() method to guide the event registration process.

In fact, Views, Controllers and Table classes are automatically sent to the event manager for analysis and registration of their events.

So, if you know that a particular event is only triggered by one Controller or one Table, you can write the event handling method in that class and let it be automatically registered by Cake.

In short, you can register your listeners at any time and in as broad or finite a scope as you want. But you must register them before you trigger them.

There are several independent event managers and you may register your events with any you choose. This detail is covered here.

With all this in place your code can, at any time, trigger an event. Again as @Ermias has illustrated:

$event = new Event('Controller.Blogs.check', $this)
$this->geteventManager()->dispatch($event);

Some details here are worth discussion.

The event manager distpatch() takes an Event object. That object is constructed from three arguments:

  1. the name of the event (a ‘key’ from implementedEvents() as mentioned earlier)
  2. the current object (to provide full context for the event handler logic)
  3. an array of other data you want in your event handling logic.

This event is then sent to one of the EventManagers. I have not verified this for myself, but given that the Event is passed to a specific EventManager, and there are several different instances of that class, you probably have to insure your event is registered on the manager you eventually trigger.

You’ll have to confirm that. It might be that ALL managers get notified, but I just don’t know.

1 Like