M
M
Maxim2019-02-11 14:20:02
Yii
Maxim, 2019-02-11 14:20:02

YII2 modules. Subscribe to events or Mailer component in a module?

Hey! Module design often requires sending emails. Notification to admins or users. This is usually done in two ways:
1. Subscribe to the events of the application component for the distribution and implement the distribution.
2. A special Mailer component is implemented, which is hardwired into the business logic and configured in the module. Example https://github.com/dektrium/yii2-user/blob/master/... And if you need to change it, then change it through the DI container.
This leads to two questions:
1. Which option is better to use? I think the first one is better.
2. If we use the first option, then where to hang events on it? In config or somewhere else?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Viktor, 2019-02-11
@myks92

IMHO, the components that implement the main business logic and mailings should not know about each other. It is better to write a class that knows that there are events for which letters should be sent (let's call it MailEventManager). Plus to add the interface for classes - mail events. For example, let's say they return the body of the email, the sender, the recipient, and the subject. And for each event, we write a class that implements this interface. And MailEventManager maps the event that occurred (eg, user registration) and the existing mail event class, from which it receives all the necessary data. The received data is already transferred to the component that directly sends letters (or notifications in telegrams, or anything else).
Thus, a greater flexibility of the system is achieved, parts of which, if necessary, can be painlessly moved into separate applications.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question