X
X
xbcaax2015-09-08 23:46:48
Yii
xbcaax, 2015-09-08 23:46:48

Why do we need events in yii2?

Does it make sense to use custom events in yii2? Isn't it easier to call the desired method from where we hang the trigger? Elegant code?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
H
HaruAtari, 2015-09-09
@xbcaax

The event model helps to build a loosely coupled system. An example from life. I am currently working on a SAAS platform. The company buys an account and chooses which modules to pay for. There are modules for accounting, checks, for personnel officers, etc. Modules must interact with each other, but any module may not exist.
For example, when creating an employee in the "personnel reserve" module, it must be automatically added to the "accounting" module. I cannot directly pull a method from another module. it is not known in advance whether it will be bought or not.
Instead, I throw an event of type user.created, and in the accounting module, in the handler of this event, I add the user to the necessary lists. It turns out that if there is no module, then the event will simply be thrown to idle, and if there is, then the handler will be launched.
It's actually very convenient. But there is also a negative side: due to weak coupling, it becomes more difficult to navigate through the code. To find out where there are handlers, you have to use the search by event name. But this is a small price to pay for the flexibility that events provide.

L
LAV45, 2015-09-11
@LAV45

This and many other topics on yii2 on the fingers, and in Russian))
https://www.youtube.com/user/ElisDN/videos

M
Mikhail Osher, 2015-09-08
@miraage

For example, there is a "user created" event.
The handlers "send an SMS somewhere", "send an event to some mix-panel", "prepare some emails for the user" are hung on it.
In order not to shit-code everything in one place, everything is divided into logical services.

N
Nikita, 2015-09-09
@bitver

Everything is quite simple. With the help of events, you can add functionality without affecting someone else's code. This adds flexibility in modularity.
In particular, look at third-party Behaviors, they show the essence of the issue very clearly.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question