jobsfan
September 28, 2021, 7:51am
1
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
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:
Ok, the upper example with LazyListenerAggregate works only when the ‘event’ attribute is one of the MVC events (The MvcEvent ).
When I have “custom” event then listener needs to be attached to Shared Event Manager.
Example of registering listeners:
<?php
namespace Order;
use Laminas\Config;
use Laminas\Mvc\MvcEvent;
use Laminas\EventManager\LazyListenerAggregate;
use Laminas\EventManager\LazyListener;
use Laminas\Mvc\Controller\AbstractController;
class Module
{
public function onBoots…
jobsfan
September 28, 2021, 8:28am
4
I just learned from this page Quick Start - laminas-eventmanager - Laminas Docs
See the first demo. Exactly the same with this.
jobsfan
September 28, 2021, 8:31am
5
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');
}
jobsfan
September 28, 2021, 8:50am
7
In your last suggestion
I am using laminas to build a project: a discourse kind system.
Our PM propose a function that publish a kind of topic(not all topic, just some choosen ones), when users reply to these topics, they may win a prize (a game activation code, etc) by chance.
To me, I want it to be decoupled to the main disourse logic. It should be a plugin-kind function.
It not break into the existing logic. And can be extented more such kind functions, and can be removed easily.
When a reply won the prize, this…
Hello,
I’m migrating quite a big project from ZF2 to Laminas and I’m nearly there.
In the older version, I have an EmailModel which is accessed by various different controllers in order to send emails, with variables like ‘account name’ @ ‘domain name’ from the config files, which used to be accessed using the ServiceManager with a line like $config = $this->getServiceLocator()->get(‘Configuration’); within the EmailModel.
What is the best way to replace this?
I’ve got all the Controllers w…
Use events for this. You have access to the event manager in a controller and you can trigger an event that a comment is added. A listener can listen on this event and send an email, write a log, and more.
And if the channel for sending the notification changes in the future, just add a new listener. The triggers do not have to be changed.
It would be even better if you use the event system in your existing model. For example, when a comment is added to your model, you trigger an event.
Then you are decoupled from controllers and request handlers.
(I will try to add a code example later.)
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.
Inject the event manager from the application via factory to your model, service or where ever it is needed.
jobsfan
September 28, 2021, 9:02am
9
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.
jobsfan
September 28, 2021, 9:18am
10
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.