S
S
Sergey Pozdnyakov2017-09-19 13:52:44
Zend Framework
Sergey Pozdnyakov, 2017-09-19 13:52:44

How to use events in ZF2/3?

Good day!
Please tell me how to subscribe to events and in what place. There are a lot of examples on the Internet with Foo bar, but they do not give any idea how to use it in real projects.
For example, I have an order change. In the bootstrap of the module, I did this

$sharedEventManager->attach(
            Mapper\Order::class,
            AbstractMapper::EVENT_INSERT_ENTITY,
            function ($e) use ($serviceManager) {
                /** @var \Office\Service\Order $orderService */
                $orderService = $serviceManager->get(Service\Order::class);
                $entity = $e->getParam('entity');
                if ($entity) {
                    $orderService->addOrderLog($entity, Entity\Order\LogAction::CREATED);
                }
            }
        );

Everything seems to be ok, it works. The event occurs only in the order mapper. In this case, maybe you need to use the usual event manager?
Further, a bunch of events arise in the service for creation / modification / delivery, etc. order. I read the manual, you need to make an event aggregator for subscribing in a bunch.
Made. The subscription goes like this.
/**
     * @param EventManagerInterface $events
     */
    public function attach(EventManagerInterface $events)
    {
        $this->listeners[] = $events->attach(
            Order::EVENT_ERROR_CONFIRM_ORDER, [
                $this,
                'sendErrorConfirmOrder'
            ]
        );
        $this->listeners[] = $events->attach(
            Order::EVENT_CONFIRM_ORDER, [
                $this,
                'okConfirmOrder'
            ]
        );
    }

But how to install the aggregator? If an event occurs in several controllers, then you need to write something like this in their factories.
$class->getEventManager()->attach($agregator);
Not quite flexible, I forgot to add the setting - the events in the moon are gone. And I need to transfer them between modules.
I tried SharedEventManager again, but then the bootstrap gets a huge canvas of code with function ($e) use ($serviceManager) {
Which is not beautiful and not convenient. How to do it right? Where can you see?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
novrm, 2017-09-20
@Afinogen

You need to understand the main point of events in ZF3 - you need to create a SEPARATE INDIVIDUAL EventManager instance for each group of events.
... and then retrieve events from that same INDIVIDUAL EventManager*.
That is, a feature with one EventManager for the ENTIRE project is a dead end.

F
Fortop, 2017-09-20
@Fortop

Firstly, the task of events is just going to the moon .
On events, you implement either an extension of the main functionality or the work of things that are optional for the main flow.
This will remove a lot of headaches both now during debugging and in the future.
Secondly, it is correct to subscribe to events at the stage of application initialization.
This can be an application bootstrap, a module bootstrap, at the time of initialization of a particular service.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question