I have got problems on laminas event management

From my last topic Which is the best entrance for a prizing discourse? When reply to a topic, will win a prize by chance, to attract more people to take part in
I learned I may need to use event-way to solve my problem.

Then I create a CommentEvents with in namespace Application\Event and place the file in Application/src/Event folder.


As well, I create a CommentListener.php also in Application/src/Event folder.

Then I registered the listener to serviceManager via the following configuration.

Finally I test it in the action(I supposed it can trigger anywhere even in the comment saving logic)

What I got is nothing!! I thought it will print the params 333,666, and type shit!!!888
But
5555
I have removed the data/cache/module-config-cache.application.config.cache.php file before I test.

I have been working on it for several days, and don’t know how to move.

Anybody can help?

Unfortunately there is no configuration for this, so this will not work.

You have misunderstood the concept, because you do not call the listener yourself, only trigger an event via the event manager.

I have a lot of work at the moment, I’ll try to create an example today.

Please see also this example:

I just learned from this page Quick Start - laminas-eventmanager - Laminas Docs
See the first demo. Exactly the same with this.

It seems will looking for such a listeners key when booting.

Ah, you wrote your own event manager, but that is not necessary and not recommended in a laminas-mvc application. Use the event manager of the application which you can access in a controller via:

public function indexAction()
{
    $this->getEventManager()->trigger('my_event');
}

In your last suggestion

I just wanna to trigger the custom event inside the db model xxxTable 's save() method, that means I thought I don’t need to pass the eventmanager from action to save() method.
I wanna to trigger the event right directly in db table’s save() method without any eventmanager param into this method.

I am trying to decouple, just new it inside the save() method, that’s what I did above. But it seems I am on the wrong direction.

In a Controller action, it is very easy to access the eventmanager via $this->getEventManager(),
However If I wanna to use in a xxxTable’s save method, should I have to pass the eventmanager into save method via a param?

I’m sorry but in this thread you use a controller:

I don’t remember what you wrote somewhere else. :wink:


Inject the event manager from the application via factory to your model, service or where ever it is needed.

Ok,

As for the listener, I can only register the listen inside the moduel.php’s onBootstrap method? It seems not graceful enough.

It’s there really no configuration key known as ‘listeners’ inside Appliction/config/module.config.php? I do find a proof as the following picture.

By the way, I following this page Quick Start - laminas-eventmanager - Laminas Docs

It seems creating a custom eventManager is useless. Before I wanna to new a instance inside a model method and trigger in the same place. I thought this way can decouple and no need to pass eventManager as param.